-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·36 lines (29 loc) · 1022 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
34
35
36
var express = require('express');
var app = express();
var fs = require('fs');
var path = require('path');
var bodyParser = require('body-parser');
app.set('port', (process.env.PORT || 3000));
app.use(express.static(path.join(__dirname, '/public')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('/favorites', function(req, res){
var data = fs.readFileSync('./data.json');
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
app.post('/favorites', function(req, res){
if(!req.body.oid || !req.body.title){
res.send("Error");
return;
}
var data = JSON.parse(fs.readFileSync('./data.json'));
data[req.body.oid] = req.body.title;
fs.writeFile('./data.json', JSON.stringify(data));
res.setHeader('Content-Type', 'application/json');
res.send(data);
});
app.listen(app.get('port'), function(){
console.log("Listening on " + app.get('port'));
});