I wrote the following code:
var express = require('express');
var router = express.Router();
var app = express();
app.put('/data', function(req, res) {
res.send("OK");
});
app.get('/data', function(req, res) {
console.log(); // What to write here?
});
app.listen(4000);
I sent a PUT request by Postman:
Now, I want to ask what I to write inside the brackets of console.log that the code will print the content stored in the http path?
I want to see the JSON
{"name":"John"}
EDIT: I tried the solution shown in this link: Node.js - get raw request body using Express:
var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require('body-parser');
app.use(function(req, res, next) {
req.rawBody = '';
req.on('data', function(chunk) {
req.rawBody += chunk;
});
next();
});
app.use(bodyParser());
app.put('/data', function(req, res) {
res.send("OK");
});
app.get('/data', function(req, res) {
console.log(res.rawBody);
res.end();
});
app.listen(4000);
I still didn't get what I expected.