Repository navigation
Expand file tree
/
Copy pathapi.js
More file actions
137 lines (109 loc) · 3.52 KB
/
Copy pathapi.js
File metadata and controls
137 lines (109 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var express = require('express');
var _ = require('underscore');
var expressValidator = require('express-validator');
var bodyParser = require('body-parser');
var app = express();
const PORT = process.env.PORT || 3000;
var todos = [
{
id: 1,
title: 'Meet mom for lunch',
completed: false
},
{
id: 2,
title: 'Cook for wife',
completed: true
}
];
var middleware = {
logger: function(req, res, next) {
console.log('Request ' + new Date().toString() + ' ' + req.method + ' ' + req.originalUrl);
next();
}
};
app.use(middleware.logger);
app.use(bodyParser.json());
app.use(expressValidator([]));
app.get('/todos/:id', function(req, res) {
if (!_.isFinite(req.params.id))
throw new Error("Invalid Id");
var id = parseInt(req.params.id, 10);
var todo = _.findWhere(todos, { id: id });
if (typeof todo !== 'undefined') {
return res.json(todo);
} else {
return res.sendStatus(404);
}
});
app.delete('/todos/:id', function(req, res) {
if (!_.isFinite(req.params.id))
throw new Error("Invalid Id");
var id = parseInt(req.params.id, 10);
var todo = _.findWhere(todos, { id: id });
if (typeof todo !== 'undefined') {
todos = _.without(todos, todo);
return res.json(todo);
} else {
return res.sendStatus(404);
}
});
app.put('/todos/:id', function(req, res) {
if (!_.isFinite(req.params.id))
throw new Error("Invalid Id");
var id = parseInt(req.params.id, 10);
var todo = _.findWhere(todos, { id: id });
if (typeof todo !== 'undefined') {
req.sanitizeBody('title').trim();
req.sanitizeBody('completed').toBoolean();
req.checkBody('title', 'Please provide a title').notEmpty();
req.getValidationResult().then(function(result) {
if (!result.isEmpty()) {
var errors = _.reduce(result.array(), function(message, error) { return message + '<br /><strong>' + error.param.toUpperCase() + '</strong> - ' + error.msg; }, '');
return res.status(400).send('There have been validation errors: ' + errors);
} else {
var body = _.pick(req.body, 'title', 'completed');
_.extend(todo, body);
return res.json(todo);
}
});
} else {
return res.sendStatus(404);
}
});
app.get('/todos', function(req, res) {
var results = todos;
if (req.query.hasOwnProperty('completed'))
results = _.where(results, { completed: req.sanitizeQuery('completed').toBoolean() });
if (req.query.hasOwnProperty('title') && req.query.title.trim().length > 0)
results = _.filter(results, function(todo) {
if (todo.title.toLowerCase().indexOf(req.query.title.trim().toLowerCase()) >= 0)
return true;
});
return res.json(results);
});
app.post('/todos', function(req, res) {
req.sanitizeBody('title').trim();
req.sanitizeBody('completed').toBoolean();
req.checkBody('title', 'Please provide a title').notEmpty();
req.getValidationResult().then(function(result) {
if (!result.isEmpty()) {
var errors = _.reduce(result.array(), function(message, error) { return message + '<br /><strong>' + error.param.toUpperCase() + '</strong> - ' + error.msg; }, '');
return res.status(400).send('There have been validation errors: ' + errors);
} else {
var body = _.pick(req.body, 'title', 'completed');
var todo = _.extend({ id: _.max(todos, function(item) { return item.id; }).id + 1 }, body);
todos.push(todo);
return res.json(todo);
}
});
});
// Setup Asset Folders
app.use(express.static(__dirname + '/public'));
// Setup Catch-All for 404
app.get('*', function(req, res){
return res.sendStatus(404);
});
app.listen(PORT, function() {
console.log("Application started");
});