I get the error that the body is not defined in expres? I get the error that the body is not defined in expres? mongoose mongoose

I get the error that the body is not defined in expres?


  1. In your route, you have to return a response other wise the request will hang:

main.js

router.post('/post/post', (req,res)=>{    console.log(req.body)    const resObject = {        ... do something with req.body ...    };    return res.status(201).json(resObject);});
  1. In your app entrypoint, set your routes after your config and middleware:

app.js

const express = require('express')const path = require('path')const app = express()const port= 3000const hostname =  '127.0.0.1'const mongoose = require('mongoose')const main = require('./routes/main')var bodyParser  = require('body-parser');/* config */mongoose.connect('mongodb://127.0.0.1/nodemon_db',{    useNewUrlParser: true,    useUnifiedTopology:true})/* Middleware */app.use(express.static('static'))app.engine('handlebars', require('exphbs'))app.set('view engine','handlebars')app.use(bodyParser.urlencoded({ extended: false }))app.use(bodyParser.json())/* Routes */app.use('/', main)app.listen(port, hostname, () => console.log(`Example app listening on port http://${hostname}:${port}/`)