Working with node.js, this error is bugging me, I guess it's something woring here with the express: var app = express();
http.createServer(app)
?? Any advice?
Listening to port 8000
Request / from 127.0.0.1 to localhost:8000
http.js:686
throw new Error('Can\'t render headers after they are sent to the client.'
^
Error: Can't render headers after they are sent to the client.
code here:
var sys = require('sys'),
http = require('http');
var express = require('express'),
routes = require('./routes');
var app = express();
var _self = this;
var _routes = {
'/' : function(request, response) {
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('hello world\n');
response.end();
},
'/is_up' : function(request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('ok');
response.end();
},
};
var _requestHandler = function(request, response) {
console.log('Request '+request.url+' from '+request.connection.remoteAddress+' to '+request.headers.host);
if(_routes[request.url] === undefined) {
response.writeHead(404, {'Content-Type': 'text/plain'});
response.write('not found\n');
//response.end();
} else {
_routes[request.url].call(this, request, response);
}
};
var _server = module.exports = http.createServer(app).
addListener('request', _requestHandler)
.listen(8000);
console.log('Listening to port ' + "8000");
I believe the problem is due to the way you are creating the server. It looks like you have two handlers and one is conflicting with the other.
In your code you are passing the Express app to it rather than your request handler and then adding a listener with your request handler.
Change it to some thing like this:
var _server = http.createServer(_requestHandler);
//_server.addListener('request', _requestHandler);
_server.listen(8000);
console.log('Listening to port ' + "8000");