Node.js / Express / Mocha / Supertest Rest API - 空请求体 [英] Node.js / Express / Mocha / Supertest Rest API - Empty Request Body
问题描述
问题是当我尝试获取 POST
/ createQuestion
API,正文为空/未定义。我收到这样的错误无法从API中读取未定义的未定义的
的问题。
Express API:
app.post(/ createQuestion,function(req,res){
var questionType = req.body .question.type;
var questionText = req.body.question.text;
var questionDuringClass = req.body.question.duringClass;
//做一堆东西
res.send(response);
});
测试:
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var winston = require('winston');
request = request('http:// localhost:8080');
describe('Questions',function(){//测试套件
before(function(done){
done();
});
它('应该创建一个freeResponse问题',function(done){//测试用例
var postData = {
question:{
type freeResponse,
text:这是一个测试freeResponse问题(自动测试),
duringClass:1
}
};
request()
.post('/ createQuestion')
.send(postData)
.expect(200)
.end(function(err,res ){// .end处理响应
if(err){
return done(err);
}
done();
} );
});
它('应该删除一个freeResponse问题',function(done){//测试用例
var postData = {
question:{
type:freeResponse,
text:这是一个测试freeResponse问题(自动测试),
duringClass:1
}
};
request()
.post('/ deleteQuestion')
.send(postData)
.expect(200)
.end(function err,res){// .end处理响应
if(err){
return done(err);
}
done();
});
});
我缺少什么? .send()
以不同的格式发送 POST
数据?
>这可能是你的应用程序没有使用bodyParser中间件。
app.use(express.bodyParser());
从expressjs文档:
req.body
此属性是包含已解析请求体的对象。该功能由bodyParser()中间件提供,尽管其他身体解析中间件也可能遵循此约定。当使用bodyParser()时,此属性的默认值为{}。
这里有一个完整的例子
var express = require('express');
var request = require('supertest');
var assert = require('assert');
var app = express();
app.use(express.bodyParser());
app.get('/',function(req,res){
res.send('ok');
});
app.post('/ createQuestion',function(req,res){
var message = req.body.partA +''+ req.body.partB;
res.send(message);
});
描述('测试一个简单的应用程序',function(){
它('应该返回代码200',功能(完成){
请求(app)
.get('/')
.expect(200)
.end(function(err,res){
if(err){
done(err);
} else {
done();
}
});
});
它('应该返回相同的发送参数连接',功能(完成){
请求(app)
.post('/ createQuestion')
.send({partA:'Hello',partB:'World'})
.expect(200,'Hello World')
.end(function(err,res){
if(err){
done(err);
} else {
done();
}
});
});
});
I've looked everywhere I can to find a solution to this. The only thing I've found is an unanswered post. I apologize if I've overlooked something.
The problem is that when I try to get the POST
values in the /createQuestion
API, the body is empty/undefined. I get errors like this Cannot read proprety 'question' of undefined
coming from the API.
The Express API:
app.post("/createQuestion", function(req, res) {
var questionType = req.body.question.type;
var questionText = req.body.question.text;
var questionDuringClass = req.body.question.duringClass;
// Do a bunch of stuff
res.send(response);
});
The test:
var should = require('should');
var assert = require('assert');
var request = require('supertest');
var winston = require('winston');
request = request('http://localhost:8080');
describe('Questions', function() { // Test suite
before(function(done) {
done();
});
it('Should create a freeResponse question', function(done) { // Test case
var postData = {
"question" : {
"type" : "freeResponse",
"text" : "This is a test freeResponse question (automated testing)",
"duringClass" : "1"
}
};
request()
.post('/createQuestion')
.send(postData)
.expect(200)
.end(function(err, res) { // .end handles the response
if (err) {
return done(err);
}
done();
});
});
it('Should delete a freeResponse question', function(done) { // Test case
var postData = {
"question" : {
"type" : "freeResponse",
"text" : "This is a test freeResponse question (automated testing)",
"duringClass" : "1"
}
};
request()
.post('/deleteQuestion')
.send(postData)
.expect(200)
.end(function(err, res) { // .end handles the response
if (err) {
return done(err);
}
done();
});
});
What am I missing? Is the .send()
sending the POST
data in some different format? Is it not POST
ing it to the body of the request?
It's probably that your app is not using bodyParser middleware in place.
app.use(express.bodyParser());
From the expressjs docs:
req.body
This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.
Here you have a complete example
var express = require('express');
var request = require('supertest');
var assert = require('assert');
var app = express();
app.use(express.bodyParser());
app.get('/', function(req, res) {
res.send('ok');
});
app.post('/createQuestion', function(req, res) {
var message = req.body.partA + ' ' + req.body.partB;
res.send(message);
});
describe('testing a simple application', function() {
it('should return code 200', function(done) {
request(app)
.get('/')
.expect(200)
.end(function(err, res){
if(err) {
done(err);
} else {
done();
}
});
});
it('should return the same sent params concatenated', function(done) {
request(app)
.post('/createQuestion')
.send({ partA: 'Hello', partB: 'World'})
.expect(200, 'Hello World')
.end(function(err, res){
if(err) {
done(err);
} else {
done();
}
});
});
});
这篇关于Node.js / Express / Mocha / Supertest Rest API - 空请求体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!