我有这个作为我的快速服务器的配置
app.use(app.router);
app.use(express.cookieParser());
app.use(express.session({ secret: "keyboard cat" }));
app.set('view engine', 'ejs');
app.set("view options", { layout: true });
//Handles post requests
app.use(express.bodyParser());
//Handles put requests
app.use(express.methodOverride());
但是当我在我的路由中请求req.body.something时,我得到了一些错误,指出body是未定义的。下面是一个使用req的路由示例。身体:
app.post('/admin', function(req, res){
console.log(req.body.name);
});
我读到这个问题是由缺乏app.use(express.bodyParser())引起的;但你可以看到,我把它叫做路线之前。
有线索吗?
我的是一个文本输入,我在这里添加了这个答案,所以它会帮助人们。确保在解析时设置了编码!我努力让它工作,直到我给它设置了一个合适的值。
这是我在没有使用任何解析器的情况下得到的错误:
error info: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.
Received an instance of undefined at Function.from (buffer.js:327:9)
我们现在不必像其他人已经提到的那样在Express中使用body解析器,而只需使用app.use(Express .text());没能解决我的问题。
undefined现在变成了Object。根据Express文档,如果Content-Type不匹配(在其他情况下),请求主体将返回一个空对象({})。
error info: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer, ArrayBuffer, or Array or an Array-like Object.
Received an instance of Object at Function.from (buffer.js:327:9)
您设置的编码类型也需要正确。在我的例子中,它是文本/纯文本。您可以更改它以满足您的需要,如JSON等。我这样做了,瞧!效果好极了!
app.use(express.text({
type: "text/plain"
}));
历史:
早期版本的Express曾经捆绑了许多中间件。bodyParser是附带的中间件之一。当Express 4.0发布时,他们决定从Express中移除捆绑的中间件,并将它们作为单独的包。在安装bodyParser模块后,语法从app.use(express.json())改为app.use(bodyParser.json())。
bodyParser在4.16.0版本中被添加回Express,因为人们希望它像以前一样与Express捆绑在一起。这意味着如果您使用的是最新版本,则不必再使用bodyParser.json()。您可以使用express.json()代替。
对于那些感兴趣的人来说,4.16.0的发布历史在这里,拉请求在这里。
好吧,言归正传,
实现:
你需要加的就是加,
app.use(express.json());
app.use(express.urlencoded({ extended: true}));
app.use(app.router); // Route will be at the end of parser
并删除bodyParser(在新版本的express中不需要)
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
快递公司会处理您的要求。:)
完整的例子是这样的,
const express = require('express')
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: true}));
app.post('/test-url', (req, res) => {
console.log(req.body)
return res.send("went well")
})
app.listen(3000, () => {
console.log("running on port 3000")
})
以防有人遇到和我一样的问题;我使用一个url前缀像
http://example.com/api/
哪一个是用路由器设置的
app.use('/api', router);
然后我有了以下内容
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
修复我的问题是将bodyparser配置放在app.use('/api', router)之上;
最后
// setup bodyparser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//this is a fix for the prefix of example.com/api/ so we dont need to code the prefix in every route
app.use('/api', router);
首先,确保在路由之前应用这个中间件(express.urlencoded)。
let app = express();
//response as Json
app.use(express.json());
//Parse x-www-form-urlencoded request into req.body
app.use(express.urlencoded({ extended: true }));
app.post('/test',(req,res)=>{
res.json(req.body);
});
代码express.urlencoded({extended:true})只响应x-www-form-urlencoded post请求,所以在ajax/XMLHttpRequest/fetch中,确保您正在发送请求。setRequestHeader(“内容类型”、“应用程序/ x-www-form-urlencoded”);头。
就是这样!