Skip to content

Instantly share code, notes, and snippets.

@janbiasi
Created October 26, 2016 07:32
Show Gist options
  • Save janbiasi/8d65956bc79c7b131c647accb8139093 to your computer and use it in GitHub Desktop.
Save janbiasi/8d65956bc79c7b131c647accb8139093 to your computer and use it in GitHub Desktop.
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