// If you are using Express 4.16+ you don't have to import body-parser anymore.
// You can do it just like this:
app.use(express.urlencoded({extended: true}));
app.use(express.json()) // To parse the incoming requests with JSON payloads
npm install body-parser
// then in your app
var express = require('express')
var bodyParser = require('body-parser')
var app = express()
// create application/json parser
var jsonParser = bodyParser.json()
// create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false })
// POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
res.send('welcome, ' + req.body.username)
})
// POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
// create user in req.body
})
const express = require("express");
const app = express();
const port = 3002;
app.use(express.json()); // you need the body parser middleware
// which already comes in express as a method
app.get("/", (req, res) => res.send("Hello World!"));
app.post('/', function (req, res) {
const { data } = req.body;
// post:
// {
// "data": {
// "name": "Jhon",
// "age": 25
// }
// }
res.send('POST request to the homepage')
})
app.listen(port, () => console.log(`Example app listening on port ${port}!`));