1

express js the body undefined error I shared the files below. I got very angry. No error appears. Please help me. Although I do it again I get the same error I searched a lot but none of the solutions worked.

main.js

                    const express = require('express')
                    const router = express.Router()


                    router.get('/', (req,res)=>{
                        res.render('site/index')
                    })

                    router.get('/login', (req,res)=>{
                        res.render('site/login')
                    })

                    router.get('/register', (req,res)=>{
                        res.render('site/register')
                    })

                    router.get('/postcreate', (req,res)=>{
                        res.render('site/postcreatepage')
                    })

                    router.get('/postupdate', (req,res)=>{
                        res.render('site/postupdatepage')
                    })

                    router.post('/post/post', (req,res)=>{
                        console.log(req.body)
                    })




                    module.exports = router

I checked and searched many times could not solve

app.js

        const express = require('express')
        const path = require('path')
        const app = express()
        const port= 3000
        const hostname =  '127.0.0.1'
        const mongoose = require('mongoose')
        const main = require('./routes/main')
        var bodyParser  = require('body-parser');



        app.use('/', main)

        app.use(express.static('static'))





        mongoose.connect('mongodb://127.0.0.1/nodemon_db',{
            useNewUrlParser: true,
            useUnifiedTopology:true
        })


        app.engine('handlebars', require('exphbs'))
        app.set('view engine','handlebars')
        

        app.use(bodyParser.urlencoded({ extended: false }))

        // parse application/json
        app.use(bodyParser.json())

        app.listen(port, hostname, ()=> console.log(`Example app listening on port http://${hostname}:${port}/`))

Although I do it again I get the same error

html

        <html>
        <head>

        </head>
        <body>
            
        <form action="post/post" method="post">

        <input  name="title"><br>

        <input   name="content"><br>
        <button type="submit">But</button>
        </form>
                
        </body> 
        </html>
sabcan
  • 407
  • 3
  • 15
  • apply your vendor middlewares **before** you apply the routes, the order does matter. also in the route you say does not work your not responding so is going to hang – Lawrence Cherone Sep 11 '20 at 21:37
  • How so can you give an example – sabcan Sep 11 '20 at 22:07
  • Similar to the answer here [enter link description here](https://stackoverflow.com/questions/44861517/express-body-parser-req-body-with-formdata-is-empty-object) I believe you need body parser – Ronald M. Kasendwa Sep 11 '20 at 22:17

1 Answers1

1
  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= 3000
const 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}/`)
rantao
  • 1,621
  • 4
  • 14
  • 34