Skip to content

Commit

Permalink
nodejs in 10 minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
olatundeG committed Dec 15, 2016
0 parents commit 7e9c007
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
logs
*.log
npm-debug.log*
node_modules
.npm
.DS_Store
51 changes: 51 additions & 0 deletions api/controllers/todoListController.js
Original file line number Diff line number Diff line change
@@ -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' });
});
};
26 changes: 26 additions & 0 deletions api/models/todoListModel.js
Original file line number Diff line number Diff line change
@@ -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);
15 changes: 15 additions & 0 deletions api/routes/todoListRoutes.js
Original file line number Diff line number Diff line change
@@ -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);
};
33 changes: 33 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
25 changes: 25 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 7e9c007

Please sign in to comment.