Express 如何同时使用bodyParser URL编码(Postman)和bodyParser json(curl)处理Expess4
我的express4服务器上有以下代码:Express 如何同时使用bodyParser URL编码(Postman)和bodyParser json(curl)处理Expess4,express,Express,我的express4服务器上有以下代码: // configure the app to use bodyParser() /*app.use(bodyParser.urlencoded({ extended: true }));*/ app.use(bodyParser.json()); 当我评论第一个集团时,我可以发送数据: curl -i -X POST -H "Content-Type:application/json" -d '{ "firstname" : "Frodo
// configure the app to use bodyParser()
/*app.use(bodyParser.urlencoded({
extended: true
}));*/
app.use(bodyParser.json());
当我评论第一个集团时,我可以发送数据:
curl -i -X POST -H "Content-Type:application/json" -d '{ "firstname" : "Frodo", "lastname" : "Baggins" }' http://localhost:3000/accounts
它工作得很好,但是当我和邮递员一起发送firstname和lastname文件,其中包含x-www-form-urlencoded和数据时,就不起作用了
但如果我再次启用代码,请执行以下操作:
app.use(bodyParser.urlencoded({
extended: true
}));
邮递员工作得很好,但不是卷发
有一种方法可以让这两种方法一起工作吗?因此,我可以通过示例使用angular2代替curl。您可以像这样链接body解析器:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
然后它将检测使用哪一个。您可以像这样链接正文解析器:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
然后它将检测使用哪一个。重要的是要注意,如果您使用
app.use(bodyParser.json({ type: '*/*' }));
app.use(bodyParser.urlencoded({ extended: true }));
主体解析器必须是这样才能工作的
app.use(bodyParser.json());
重要的是要注意,如果您使用
app.use(bodyParser.json({ type: '*/*' }));
app.use(bodyParser.urlencoded({ extended: true }));
主体解析器必须是这样才能工作的
app.use(bodyParser.json());
很好,很有效!!但是没有读过任何书或教程!在哪里可以找到所有这些技巧?@stackdave-这曾经是默认行为,但他们从未说过在更改构造函数时可以将它们链接起来。所以你只需要知道,或者在StackOverflow上询问:-)很好,它很有效!!但是没有读过任何书或教程!在哪里可以找到所有这些技巧?@stackdave-这曾经是默认行为,但他们从未说过在更改构造函数时可以将它们链接起来。因此,您只需知道,或询问StackOverflow:-)