问题描述
我看到,LoopBack内置了Express 3.x中间件。实际上,身体解析器位于 loopback / node_modules
中。但是我不知道如何使用它作为中间件。我从来没有使用Express 3.x,所以也许就是这样。 require
不行,显然,除非我在我的项目中安装body-parser作为依赖。
I see that LoopBack has the Express 3.x middleware built-in. Indeed, body-parser is in loopback/node_modules
. But I cannot figure out how to use it as middleware. I have never worked with Express 3.x, so maybe it's just that. require
does not work, obviously, unless I install body-parser as a dependency in my project.
我应该在 server.js
中使用身体解析器,以便将Web表单解析成 req.params
?
What should I do in server.js
to use body-parser so that web forms are parsed into req.params
? That's what it does, right?
推荐答案
经过几个小时的挫折,我刚加入到中间件。 json
像这样:
After hours of frustration, I just added it to middleware.json
like so:
"parse": {
"body-parser#json": {},
"body-parser#urlencoded": {"params": { "extended": true }}
}
它作为依赖项安装。现在我的路线中有 req.body
中的表单数据。我的 server / boot / routes.js
如下所示:
It is installed as a dependency. Now I have form data in req.body
in my routes. My server/boot/routes.js
looks like this:
module.exports = function(app) {
app.post('/mailing_list', function(req, res) {
console.log(req.body.email);
res.send({"status": 1, "message": "Successfully added to mailing list."})
});
}
这篇关于如何在LoopBack中使用body-parser?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!