Pregunta

I am new to node.js and began by creating a small web app which runs at port 5000. When i tried this url which runs in my local(either via the browser or via curl), everything works fine, and i get back the response. But when i tried connecting it with the http client while doing 'BDD using vows', the test broke and the resulting message was

✗ 

  /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)

The response here is undefined.

body = "Not found"; 
response.writeHead(404, {'Content-Type': 'text/plain', 'Content-Length': body.length});     
response.end(body);

This is how i am responding within the application. I have set the headers content-type and content-length. Could any one please help me out as to what might be the issue?

The vow that i have written is this.

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(); 
¿Fue útil?

Solución

Try changing

'should respond with 404': function(e,res) {
  assert.equal(res.status, 404);
}

to

'should respond with 404': function(res) {
  assert.equal(res.status, 404);
}

According to the Node.js HTTP docs, the http.request (and thus http.get) callbacks do not take an err argument.

Otros consejos

I made a change in the code and tried with response as the first parameter of the callback and error as the second. This worked for me.

'should respond with 404': function(res,e) {
   assert.equal(res.status, 404);
}

However res as the only parameter still raises the error as i had mentioned above.
Thank you for the support.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top