未定义节点js req.body

时间:2019-02-20 20:35:52

标签: node.js post

首先,我尝试了thisthisthis

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

app.post('/user_create', (req,res) => {
    console.log("trying to create a user "+req.body);

    const firstName = req.body.first_name
    const lastName = req.body.last_name

    console.log("first name is "+firstName+" "+lastName);

    res.end()
})

现在,在尝试了上面给出的所有文章之后,我仍然需要req.body未定义,我是Nodejs中的新手。我正在使用express 4.16.4和body-parser 1.18.3。

1 个答案:

答案 0 :(得分:0)

let options = {
    headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded'),
};