Skip to content

Instantly share code, notes, and snippets.

@iuliaL
Last active May 3, 2016 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iuliaL/86806b54da7058d6dee8 to your computer and use it in GitHub Desktop.
Save iuliaL/86806b54da7058d6dee8 to your computer and use it in GitHub Desktop.
var express = require('express');
var fs = require('fs');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var events = JSON.parse(fs.readFileSync("data.json",'utf8'));
var counter = Object.keys(events).length;
console.log(events);
app.get('/events', function (req, res) {
events = JSON.parse(fs.readFileSync("data.json",'utf8'));
res.send(events);
});
app.get('/event/:id', function (req, res) {
events = JSON.parse(fs.readFileSync("data.json",'utf8'));
res.send(events[req.params.id]);
});
app.post('/events', function (req, res) {
events = JSON.parse(fs.readFileSync("data.json",'utf8'));
counter++;
req.body.id = counter;
events[counter] = req.body;
fs.writeFileSync("data.json", JSON.stringify(events), 'utf8');
res.send(counter);
});
app.put('/event/:id', function (req, res) {
events = JSON.parse(fs.readFileSync("data.json",'utf8'));
req.body.id = req.params.id;
events[req.params.id] = req.body;
fs.writeFileSync("data.json", JSON.stringify(events), 'utf8');
res.send(events[req.params.id]);
});
app.delete('/event/:id', function (req, res) {
events = JSON.parse(fs.readFileSync("data.json",'utf8'));
delete events[req.params.id];
fs.writeFileSync("data.json", JSON.stringify(events), 'utf8');
res.send("ok");
});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment