From 7e9c0075c25ea1f3ede1739d915995df3f0783a2 Mon Sep 17 00:00:00 2001 From: Olatunde Garuba Date: Thu, 15 Dec 2016 03:12:14 +0100 Subject: [PATCH] nodejs in 10 minutes --- .gitignore | 6 ++++ api/controllers/todoListController.js | 51 +++++++++++++++++++++++++++ api/models/todoListModel.js | 26 ++++++++++++++ api/routes/todoListRoutes.js | 15 ++++++++ package.json | 33 +++++++++++++++++ server.js | 25 +++++++++++++ 6 files changed, 156 insertions(+) create mode 100644 .gitignore create mode 100644 api/controllers/todoListController.js create mode 100644 api/models/todoListModel.js create mode 100644 api/routes/todoListRoutes.js create mode 100644 package.json create mode 100644 server.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf09a8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +logs +*.log +npm-debug.log* +node_modules +.npm +.DS_Store diff --git a/api/controllers/todoListController.js b/api/controllers/todoListController.js new file mode 100644 index 0000000..f267eef --- /dev/null +++ b/api/controllers/todoListController.js @@ -0,0 +1,51 @@ +'use strict'; + +var mongoose = require('mongoose'), + Task = mongoose.model('Tasks'); + + + +exports.list_all_tasks = function(req, res) { + Task.find({}, function(err, task) { + if (err) + res.send(err); + res.json(task); + }); +}; + + +exports.create_a_task = function(req, res) { + var new_task = new Task(req.body); + new_task.save(function(err, task) { + if (err) + res.send(err); + res.json(task); + }); +}; + +exports.read_a_task = function(req, res) { + Task.findById(req.params.taskId, function(err, task) { + if (err) + res.send(err); + res.json(task); + }); +}; + +exports.update_a_task = function(req, res) { + Task.findOneAndUpdate(req.params.taskId, req.body, {new: true}, function(err, task) { + if (err) + res.send(err); + res.json(task); + }); +}; +// Task.remove({}).exec(function(){}); +exports.delete_a_task = function(req, res) { + + Task.remove({ + _id: req.params.taskId + }, function(err, task) { + if (err) + res.send(err); + res.json({ message: 'Task successfully deleted' }); + }); +}; diff --git a/api/models/todoListModel.js b/api/models/todoListModel.js new file mode 100644 index 0000000..00f45cc --- /dev/null +++ b/api/models/todoListModel.js @@ -0,0 +1,26 @@ +'use strict'; + + +var mongoose = require('mongoose'); +var Schema = mongoose.Schema; + +var TaskSchema = new Schema({ + name: { + type: String, + Required: 'Kindly enter the name of the task' + }, + Created_date: { + type: Date, + default: Date.now + }, + status: { + type: [{ + type: String, + enum: ['pending', 'ongoing', 'completed'] + }], + default: ['pending'] + } +}); + + +module.exports = mongoose.model('Tasks', TaskSchema); \ No newline at end of file diff --git a/api/routes/todoListRoutes.js b/api/routes/todoListRoutes.js new file mode 100644 index 0000000..c4f5300 --- /dev/null +++ b/api/routes/todoListRoutes.js @@ -0,0 +1,15 @@ +'use strict'; + +module.exports = function(app) { + var todoList = require('../controllers/todoListController'); + + // todoList Routes + app.route('/task') + .get(todoList.list_all_tasks) + .post(todoList.create_a_task); + + app.route('/task/:taskId') + .get(todoList.read_a_task) + .put(todoList.update_a_task) + .delete(todoList.delete_a_task); +}; diff --git a/package.json b/package.json new file mode 100644 index 0000000..f9032a9 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "todolistapi", + "version": "1.0.0", + "description": "RESTful todoListApi", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "nodemon server.js" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/generalgmt/RESTfulAPITutorial.git" + }, + "keywords": [ + "RESTful", + "API", + "Tutorial" + ], + "author": "olatunde garuba", + "license": "ISC", + "bugs": { + "url": "https://github.com/generalgmt/RESTfulAPITutorial/issues" + }, + "homepage": "https://github.com/generalgmt/RESTfulAPITutorial#readme", + "devDependencies": { + "nodemon": "^1.11.0" + }, + "dependencies": { + "body-parser": "^1.15.2", + "express": "^4.14.0", + "mongoose": "^4.7.2" + } +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..b0bbb57 --- /dev/null +++ b/server.js @@ -0,0 +1,25 @@ +var express = require('express'), + app = express(), + port = process.env.PORT || 3000, + mongoose = require('mongoose'), + Task = require('./api/models/todoListModel'), + bodyParser = require('body-parser'); + +mongoose.Promise = global.Promise; +mongoose.connect('mongodb://localhost/Tododb'); + + +app.use(bodyParser.urlencoded({ extended: true })); +app.use(bodyParser.json()); + + +var routes = require('./api/routes/todoListRoutes'); +routes(app); + +app.use(function(req, res) { + res.status(404).send({url: req.originalUrl + ' not found'}) +}); + +app.listen(port); + +console.log('todo list RESTful API server started on: ' + port);