-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathserver.js
66 lines (50 loc) · 1.28 KB
/
server.js
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
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var app = express();
app.use(bodyParser.json());
app.use(logger());
app.use('/static', express.static(__dirname + '/dist'));
app.get('/page*', function(req, res) {
return res.sendFile('dist/index.html', {
root: __dirname
});
});
var id = 0;
var data = [];
app.get('/api/feed', function(request, response) {
setTimeout(function() {
response.status(200).send({
data: data,
total: data.length
});
}, 1000);
});
app.post('/api/feed', function(request, response) {
var newObject = {
id: id++,
text: request.body.text
};
data.push(newObject);
console.log(data);
return response.status(200).send(newObject);
});
app.put('/api/feed/:id', function(request, response) {
data.forEach(function(item) {
if (item.id !== request.body.id) {
return;
}
item.text = request.body.text;
});
console.log(data);
return response.status(200).send(request.body);
});
app.del('/api/feed/:id', function(request, response) {
data = data.filter(function(item) {
return +item.id !== +request.params.id;
});
console.log(data);
return response.status(204).send();
});
app.listen(3000);
console.log('http://localhost:3000/page');