-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (53 loc) · 1.9 KB
/
Copy pathindex.js
File metadata and controls
64 lines (53 loc) · 1.9 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
import express from 'express';
import bodyParser from 'body-parser';
import moment from 'moment';
import _ from 'lodash';
import path from 'path';
var app = express();
var dbCollection = [
{id: '0', title: 'my first task', cdate: '2015-09-08T18:39:37+03:00', desc: 'I have to finish this first task'},
{id: '1', title: 'another task', cdate: '2015-09-09T20:41:37+03:00', desc: 'I can skip this one'},
{id: '2', title: 'get beers', cdate: '2015-09-10T08:39:37+03:00', desc: 'Do not forget the beers'}
];
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.use('/public', express.static('public'));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, '../../public/index.html'));
});
app.get('/tasks', function (req, res, next) {
res.json(dbCollection);
});
app.post('/tasks', function (req, res, next) {
var id = parseInt(_.last(dbCollection).id) + 1;
id = id.toString();
var task = {
id: id,
title: req.body.title || '',
desc: req.body.desc || '',
cdate: moment().format()
};
dbCollection.push(task);
res.json(task);
});
app.get('/tasks/:task_id', function (req, res, next) {
var id = req.params.task_id;
res.json(_.find(dbCollection, {id: id}));
});
app.put('/tasks/:task_id', function (req, res, next) {
var id = req.params.task_id;
var task = _.find(dbCollection, {id: id});
task.title = req.body.title || task.title;
task.desc = req.body.desc || task.desc;
res.json(task);
});
app.delete('/tasks/:task_id', function (req, res, next) {
var id = req.params.task_id;
var removedTask = _.pullAt(dbCollection, _.findIndex(dbCollection, {id: id}));
res.json(removedTask);
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});