Created
October 26, 2016 07:32
-
-
Save janbiasi/8d65956bc79c7b131c647accb8139093 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DATASOURCE = 'data.json'; | |
const fs = require('fs'); | |
const parser = require('body-parser'); | |
const app = require('express')(); | |
app.use(parser.json()); | |
// HTTP GET /api/data | |
app.get('/api/data', (req, res, next) => { | |
fs.readFile(DATASOURCE, (err, data) => { | |
if(err) { return next(err); } | |
res.json(data); | |
}); | |
}); | |
// HTTP POST /api/data { content } | |
app.post('/api/data', (req, res, next) => { | |
fs.writeFile(DATASOURCE, req.body.content, (err) = { | |
res.json({ | |
error: err || null | |
}); | |
}); | |
}); | |
app.listen(8080, () => { | |
console.log('Server up and running ...'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment