Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js response undefined when connected using http client

Node.js response undefined when connected using http client

Problem

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. (/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. (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows.js:85:17)
    at EventEmitter. (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. (/home/sunil/work/nodal_programs/subscription-engine-processor/node_modules/vows/lib/vows/context.js:46:29)
    at ClientRequest. (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(); 
Problem courtesy of: Sunil

Solution

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.

Solution courtesy of: Michelle Tilley

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js response undefined when connected using http client

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×