express js how to post to .json file code example

Example 1: nodejs json data serving

// load up the express framework and body-parser helper
const express = require('express');
const bodyParser = require('body-parser');

// create an instance of express to serve our end points
const app = express();

// we'll load up node's built in file system helper library here
// (we'll be using this later to serve our JSON files
const fs = require('fs');

// configure our express instance with some body-parser settings
// including handling JSON data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// this is where we'll handle our various routes from
const routes = require('./routes/routes.js')(app, fs);

// finally, launch our server on port 3001.
const server = app.listen(3001, () => {
  console.log('listening on port %s...', server.address().port);
});

Example 2: nodejs json data serving

{
  "1": {
    "name": "king arthur",
    "password": "password1",
    "profession": "king",
    "id": 1
  },
  "2": {
    "name": "rob kendal",
    "password": "password3",
    "profession": "code fiddler",
    "id": 2
  },
  "3": {
    "name": "teresa may",
    "password": "password2",
    "profession": "brexit destroyer",
    "id": 6
  }
}