我是 node.js 的新手,首先创建了一个在端口 5000 上运行的小型 Web 应用程序。当我尝试在我的本地运行的这个 url(通过浏览器或通过 curl)时,一切正常,我找回了回复。但是,当我在执行“使用誓言的 BDD”时尝试将其与 http 客户端连接时,测试失败了,结果消息是
✗
/GET
✗ should respond with 404
***TypeError: Cannot read property 'status' of undefined***
at ClientRequest.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/sample-test.js:13:23)
at runTest (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:132:26)
at EventEmitter.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:85:17)
at EventEmitter.<anonymous> (events.js:67:17)
at EventEmitter.emit (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:236:24)
at /home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows/context.js:31:52
at ClientRequest.<anonymous> (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows/context.js:46:29)
at ClientRequest.<anonymous> (events.js:67:17)
at ClientRequest.emit (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:236:24)
at HTTPParser.onIncoming (http.js:1225:11)
✗ Errored » 1 errored (0.012s)
这里的响应是未定义的。
body = "Not found";
response.writeHead(404, {'Content-Type': 'text/plain', 'Content-Length': body.length});
response.end(body);
这就是我在应用程序中的响应方式。我已经设置了标题内容类型和内容长度。任何人都可以帮我解决可能是什么问题吗?
我写的誓言是这样的。
var http = require('http'),
vows = require('vows'),
assert = require('assert');
vows.describe("notification").addBatch({
"/GET": {
topic: function() {
http.get({host: 'localhost', port: 1337, path: '/', method: 'GET'}, this.callback) ;
},
'should respond with 404': function(e,res) {
assert.equal(res.status, 404);
}
}
}).run();