I have a question in node js express. My question concerns post request with node js express. I want to make a post request for this data in json form. Below you can find the node js express code and the post I want to do.
node js express code:
var express = require('express');
var app = express();
app.use(express.json()); // built-in middleware for express
app.post('/', function(request, response){
let myJson = request.body; // your JSON
let myValue = request.body.II.A; // a value from your JSON
response.send(myJson); // echo the result back
});
app.listen(3000);
======================
The body of the post in json format:
{
"I" : {
"Y" : "3",
"Z" : "2",
"T" : "1"
},
"II" : [
{
"A" : "4",
"B" : "5",
"C" : {"a": "4", "b" : "6"},
}
]
}
use this code to get body data from post API.
var express = require('express');
var bodyParser = require("body-parser");
var app = express();
app.use(express.json()); // built-in middleware for express
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/', function(request, response){
let myJson = request.body; // your JSON
let myValue = request.body.II.A; // a value from your JSON
response.send(myJson); // echo the result back
});
app.listen(3000);