express get json body code example
Example 1: body parser express
npm i body-parser
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('you posted:\n')
res.end(JSON.stringify(req.body, null, 2))
})
Example 2: parse json express
var express = require('express');
var app = express();
app.use(express.json());
app.post('/', function(request, response){
console.log(request.body);
response.send(request.body);
});
app.listen(3000);
Example 3: node express post request json
var express = require('express');
var app = express();
app.use(express.json());
app.post('/', function(request, response){
let myJson = request.body;
let myValue = request.body.myKey;
response.send(myJson);
});
app.listen(3000);
Example 4: express json body
$ npm install body-parser
Example 5: express json body
var bodyParser = require('body-parser')