host json data node.js code example
Example 1: nodejs json data serving
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const fs = require('fs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const routes = require('./routes/routes.js')(app, fs);
const server = app.listen(3001, () => {
console.log('listening on port %s...', server.address().port);
});
Example 2: nodejs json data serving
app.put('/users/:id', (req, res) => {
readFile(data => {
const userId = req.params['id'];
data[userId] = req.body;
writeFile(JSON.stringify(data, null, 2), () => {
res.status(200).send(`users id:${userId} updated`);
});
}, true);
});