连接中间件中的bodyParser()有什么作用?

Ric*_*ich 20 node.js node.js-connect

我正在做node.js的教程,课程教我如何使用node创建服务器.在下面的代码中,connect.bodyParser()行有什么作用?

var app = connect()
    .use(connect.bodyParser())
    .use(connect.static('public'))
    .use(function (req, res) {
        if (req.url === '/process') {
            res.end(req.body.name + ' would repeat ' + req.body.repeat + ' times.');
        } else {
            res.end("Invalid Request");
        }
    })
    .listen(3000);
Run Code Online (Sandbox Code Playgroud)

Nit*_*ked 15

它填充req.body(除其他外)POST参数的值.这是文档和示例:http://expressjs.com/api.html#req.body

bodyParser是"Connect"的一部分,"Connect"是node.js的一组中间件.以下是来自Connect的真实文档和来源:http://www.senchalabs.org/connect/bodyParser.html

正如您所看到的,它只是一个薄的包装器,试图解码JSON,如果失败尝试决定URLEncoded,如果失败尝试解码多部分.