主体解析器在 nodejs 中显示未定义
Body parser is showing undefined in nodejs
当我通过 url 发送数据时,主体解析器显示为未定义。
这是 url : http://localhost:3011/mybooking?email=example@example.com
这是我的代码:
var bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('*',(req,res)=>
{
console.log(req.body)
console.log(req.body.email)
console.log(req.params.email)
})
const port = 3011
app.listen(port,(req,res)=>
{
console.log("App.js listening on the port "+port)
})
请尽早帮助我。
我的输出是
App.js listening on the port 3011
{}
undefined
undefined
在 URL 中传递的变量使用 req.query 检索。
在您的情况下,要获取电子邮件的值,您需要使用 req.query.email.
进行检索
req.body在数据通过正文时使用(基本上是post请求)
req.params使用动态变量传递数据时使用
例如。 http://localhost:3011/book/:id ---> req.params.id
当我通过 url 发送数据时,主体解析器显示为未定义。
这是 url : http://localhost:3011/mybooking?email=example@example.com
这是我的代码:
var bodyParser = require('body-parser');
const express = require('express');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('*',(req,res)=>
{
console.log(req.body)
console.log(req.body.email)
console.log(req.params.email)
})
const port = 3011
app.listen(port,(req,res)=>
{
console.log("App.js listening on the port "+port)
})
请尽早帮助我。
我的输出是
App.js listening on the port 3011
{}
undefined
undefined
在 URL 中传递的变量使用 req.query 检索。
在您的情况下,要获取电子邮件的值,您需要使用 req.query.email.
进行检索req.body在数据通过正文时使用(基本上是post请求)
req.params使用动态变量传递数据时使用
例如。 http://localhost:3011/book/:id ---> req.params.id