用 chai 发送 POST 请求会发送一个空体?

Sending a POST request with chai sends an empty body?

我现在有以下设置

test.js

     var user = {
        username: 'test_user',
        email: 'test@test.me',
        password: 'you shall not pass',
        address: 'No where street'
     };
     chai.request(app)
        .post('/api/v1/users')
        .send(user);

我正在 routes/user.js

中处理 post 请求
router.post('/', function(req, res, next) {
    console.log('body: ' + req.body);
    queries.insertUser(req.body)
        .then(function(id) {
            return queries.getSingleUser(id);
        })
        .then(function(user) {
            res.status(200).json(user);
        })
        .catch(function(err) {
            next(err);
        });
});

req.body 最终未定义。关于可能出了什么问题的任何线索?

代码在 https://ide.c9.io/burtonium/node-from-scratch 上,如果有人想看的话。

req.body 未定义通常是由于未在 Express 中使用 body-parser 中间件,或者声明不正确(例如, after 路由想要访问 req.body).

假设 Chai 发送 JSON,将其添加到您的 Express 应用中:

app.use(require('body-parser').json());

(在声明路由器之前)