未显示 Express 正文解析器请求
Express body-parser req not shown
我将 Express 与 body-parser 一起使用如下:
// Import all required library
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true, limit: '500mb'}))
var port = 3000
// Route for the API
var router = new express.Router()
router.get('/videos', (req, res, next) => {
console.log('GET /' + req)
return res.json({
"Your videos"
})
})
// Using Router
app.use('/', router)
app.listen(port, function(err){
if (!err) {
console.log("API started running at port 3000")
}
})
使用的库版本是
"dependencies": {
"body-parser": "1.16.1",
"express": "4.14.1"
}
req
中没有显示任何查询参数。结果如下
GET /[object Object]
当我用POSTMAN来GET
localhost:3000/videos?type=popularity
的时候。
到目前为止,这种 api 设置一直对我有效。谁能解释一下我做错了什么?
谢谢
代替console.log('GET /' + req)
,做:
console.log('GET /', req);
您将获得完整的对象。
查询参数可通过req.query
获得:
console.log('GET /', req.query);
FWIW,body-parser
不是访问这些的要求。
我将 Express 与 body-parser 一起使用如下:
// Import all required library
var express = require('express')
var app = express()
var bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true, limit: '500mb'}))
var port = 3000
// Route for the API
var router = new express.Router()
router.get('/videos', (req, res, next) => {
console.log('GET /' + req)
return res.json({
"Your videos"
})
})
// Using Router
app.use('/', router)
app.listen(port, function(err){
if (!err) {
console.log("API started running at port 3000")
}
})
使用的库版本是
"dependencies": {
"body-parser": "1.16.1",
"express": "4.14.1"
}
req
中没有显示任何查询参数。结果如下
GET /[object Object]
当我用POSTMAN来GET
localhost:3000/videos?type=popularity
的时候。
到目前为止,这种 api 设置一直对我有效。谁能解释一下我做错了什么?
谢谢
代替console.log('GET /' + req)
,做:
console.log('GET /', req);
您将获得完整的对象。
查询参数可通过req.query
获得:
console.log('GET /', req.query);
FWIW,body-parser
不是访问这些的要求。