-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathserver.js
executable file
·33 lines (28 loc) · 979 Bytes
/
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
var fs = require('fs');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.set('port', (process.env.PORT || 3000));
app.use('/', express.static(path.join(__dirname, 'build')));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/tweets.json', function(req, res) {
fs.readFile('tweets.json', function(err, data) {
res.setHeader('Cache-Control', 'no-cache');
res.json(JSON.parse(data));
});
});
app.post('/tweets.json', function(req, res) {
fs.readFile('tweets.json', function(err, data) {
var tweets = JSON.parse(data);
tweets.unshift(req.body);
fs.writeFile('tweets.json', JSON.stringify(tweets, null, 4), function(err) {
res.setHeader('Cache-Control', 'no-cache');
res.json(tweets);
});
});
});
app.listen(app.get('port'), function() {
console.log('Server started: http://localhost:' + app.get('port') + '/');
});