How to handle http.get() when received no data in nodejs?

Let's say that I have a service that returns customer information by a given id. If it's not found I return null.

My app calls this services like this:

http.get('http://myserver/myservice/customer/123', function(res) {
    res.on('data', function(d) {
        callback(null, d);
    });
}).on('error', function(e) {
    callback(e);
});

Currently my service responds with 200 but no data.

How do I handle when no data is returned?

Should I change it to return a different http code? In this case how to handle this? I tried many different approaches without success.

First off, it's important to remember that res is a stream of data; as it stands, your code is likely to call callback multiple times with chunks of data. (The data event is fired each time new data comes in.) If your method has been working up to this point, it's only because the responses have been small enough that the response payload wasn't broken into multiple chunks.

To make your life easier, use a library that handles buffering HTTP response bodies and allows you to get the complete response. A quick search on npm reveals the request package.

var request = require('request');
request('http://server/customer/123', function (error, response, body) {
  if (!error && response.statusCode == 200) {
      callback(null, body);
  } else {
      callback(error || response.statusCode);
  }
});

As far as your service goes, if a customer ID is not found, return a 404 – it's semantically invalid to return a 200 OK when in fact there is no such customer.

Use the Client Response 'end' event:

    //...
    res.on('data', function(d) {
        callback(null, d);
    }).on('end', function() {
        console.log('RESPONSE COMPLETE!');
    });

I finally found a way to solve my problem:

http.get('http://myserver/myservice/customer/123', function(res) {
    var data = '';

    response.on('data', function(d) {
        data += d;
    });

    response.on('end', function() {
        if (data != '') {
            callback(null, data);
        }
        else {
            callback('No customer was found');
        } 
    });
}).on('error', function(e) {
    callback(e);
});

This solves the problem, but I'm also going to adopt josh3736's suggestion and return a 404 on my service when the customer is not found.