-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
33 lines (28 loc) · 832 Bytes
/
routes.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 express = require('express') ,
router = express.Router()
// Readfile from local file
router.get('/readfile', function (req, res) {
var jsonfile = require('jsonfile')
var file = __dirname + '/src/js/data.json';
jsonfile.readFile(file, function(err, obj) {
res.send(obj);
})
})
// Write to local file
router.post('/writefile', function (req, res) {
res.setHeader('Content-Type', 'application/json');
var jsonObj = req.body.json;
var jsonfile = require('jsonfile')
var file = __dirname + '/src/js/data.json';
jsonfile.writeFile(file, jsonObj, function (err) {
if(err){
console.error(err)
}
})
res.send({
status : 200,
message : "json object has been saved successfuly"
});
})
// export modules
module.exports = router