-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.js
37 lines (27 loc) · 833 Bytes
/
api.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
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
var path = require('path');
var fs = require('fs');
var threads = require('./data/threads.json');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
// Deliver threads
app.get("/getThreads", function(req, res){
res.send(JSON.stringify(threads));
});
// Save threads
app.post("/setThreads", function(req, res){
//console.log(req.body);
fs.writeFile('./data/threads.json',JSON.stringify(req.body),function(err) {
if(err) return console.error(err);
console.log('Saved threads!');
})
});
var server = app.listen(4000, function () {
var host = server.address().address;
var port = server.address().port;
console.log('Api server listening at http://%s:%s', host, port);
});