Handling POST Requests
suggest changeExpress handles GET with app.get and POST with app.post.
The body is not parsed until middleware does it. The body-parser package parses JSON and URL-encoded bodies into req.body. Express 4.16 and later includes the same parsers as express.json() and express.urlencoded().
var bodyParser = require('body-parser');
const express = require('express');
const app = express();
// Parses the body for POST, PUT, DELETE, etc.
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.post('/post-data-here', function(req, res, next){
console.log(req.body); // req.body contains the parsed body of the request.
res.end();
});
app.listen(8080, 'localhost');
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents