用chai发送一个POST请求发送一个空的正文?
我现在有以下设置
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中的发布请求
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
中间件,或者声明不正确(例如, 在想要访问req.body
的路由之后 )。
假设柴发送JSON,添加到您的快速应用程序:
app.use(require('body-parser').json());
(在你声明路由器之前)
- 使用柴和冼方法在一个方法内扼杀诺言
- Chai / Moka – > TypeError:request.get(…).expect不是一个函数
- 允许chai / mochatesting将错误冒泡到process.on
- 使用Mocha和Chai编写testing用例,以便使用Error
- Chaivariables不是由Karma / Grunt加载的
- 使用Mocha / Chai来testingNodeJS的匿名callback代码
- 向Chai / Mocha提供应包含的部分密钥列表
- JSON Stringify函数返回相等但chai的相等函数返回不相等
- 我使用毛毯在摩卡代码覆盖范围内获得0%覆盖0 SLOC