Can I cancel a HTTP response after headers are sent? - node.js

On a node HTTP server I'm spawning a process and streaming the output to the response.
When the process returns I'd like to indicate to the client if an error occured. Obviously I can't set the HTTP status code as the headers were already sent.
Is there a way to abort the connection?
E.g.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World\n');
// how do I abort the request at this point
// and indicate an error to the client?
// e.g. curl should return != 0
res.end();
}).listen(1337, '127.0.0.1');

I found this in google groups, you can use
either req.client.destroy();
or res.connection.destroy();
curl will then report
curl: (18) transfer closed with outstanding read data remaining

var thirdPartyApp = $express();
thirdPartyApp.use('/error/', function (req, res) {
console.log('error');
res.writeHead(200);
res.write('aye');
throw 'booboo!';
res.end();
});
On expressjs this does not kill the node process (probably just need to bind to the error event) but does immediately kill the response, indicating an error to the user without a timeout.

Related

Node.js HTTP Server Stops Listening

I have an HTTP server setup on port 1338 to listen on the IP for the server. This works just fine when I first launch Node, but for some reason I have run into issues where the server randomly stops listening. I have checked the logs that Forever is collecting from my app including any uncaught exceptions. Nothing shows up in the logs of having any error since startup.
My question is two fold. What would cause the server to stop listening at random intervals? Also what check should be running in Node so that I can log out the error that is causing the listener to stop?
Here is the code below for my HTTP Server.
http.createServer(function (req, res) {
var pathname = url.parse(req.url).pathname;
var query = url.parse(req.url, true).query;
var check;
var responseData = '';
if(pathname === '/healthcheck/ticket'){
check = new Date().getTime();
check = check - tickets.lastAction;
if(check < 30000){
responseData = "page ok";
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(responseData);
}
else{
check = check/1000;
responseData = 'Last action taken by the Ticket Generator was ' + check + ' seconds ago';
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(responseData);
}
}
else{
responseData = 'URL NOT FOUND!';
res.writeHead(404, {'Content-Type': 'text/plain'});
res.end(responseData);
}
}).listen(config.eng.port, config.eng.host);
You don't handle the case when pathname is not equal to /healthcheck/ticket. There is no else branch.
Hence, when your server gets called with other urls but /healthcheck/ticket, it never closes the res stream, as res.end() is never called.
After a while, your server runs out of (network) resources, and hence seems to hang (i.e., it does not react on new requests any longer).
The solution is simple: Provide an else branch, and call res.end() in it, and everything should be fine.

Difficulty understanding nodejs documentation

I'm really confused about this section
http://nodejs.org/api/http.html#http_http_createserver_requestlistener
The requestListener is a function which is automatically added to the 'request' event.
What does the term "added" specifically mean?
Also for here
http://nodejs.org/api/http.html#http_event_request
What does the the code directly beneath mean function (request, response) { }? Does it mean that that function gets passed each time there is a request?
The requestListener is a lsitener that listens to the 'request' event. Each time a request event is emitted, the requestListener is executed. You pass a function.
That function you pass, should match:
function (request, response) { }
I believe there is an example at the main page of nodejs.org.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
So each time a request-event is emitted, this function is 'called'.
function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
With req and res a parameters. (Request and response).
If it is any help the statement
var app = http.createServer( function reqlistener(request, response){...} ).listen(1337);
where the function reqlistener is the requestListener argument, is equivalent to the following
var app = http.createServer().listen(1337);
app.on('request', function reqlistener(request, response){...} );
So it is just a shortcut for providing a listener for event request during server start itself. The event request is emitted for each request once when received by the server.

how to send reponse when timeout in node.js http module

On nodejs.org socket.setTimeout, it says
When an idle timeout is triggered the socket will receive a 'timeout' event but the connection will not be severed.
But when I test code like this:
var http = require('http');
server = http.createServer(function (request, response) {
request.socket.setTimeout(500);
request.socket.on('timeout', function () {
response.writeHead(200, {'content-type': 'text/html'});
response.end('hello world');
console.log('timeout');
});
});
server.listen(8080);
The socket is closed immediately after timeout, and no data is replied to the browser. Which is quite different from the document. Is this a bug or is there any tricks dealing socket under http module?
The documentation is indeed correct, however it looks like the http module adds a 'timeout' listener which calls socket.destroy(). So what you need to do is get rid of that listener by calling request.socket.removeAllListeners('timeout').
So your code should look like:
var http = require('http');
server = http.createServer(function (request, response) {
request.socket.setTimeout(500);
request.socket.removeAllListeners('timeout');
request.socket.on('timeout', function () {
response.writeHead(200, {'content-type': 'text/html'});
response.end('hello world');
console.log('timeout');
});
});
server.listen(8080);

Unable to set headers in node.js in POST method

I have a case where i have to read the data from the request body and create a file and write the data into it. If the operation is successful I set the response header to 201 and add the location of file in Location header. The file creation is done using Java methods and node.js code is below.
var server = http.createServer(function(req, res)
{
var body = "";
req.on("data", function(chunk)
{
body += chunk.toString();
});
req.on("end", function() {
var rtn = obj.AddonPostMethod(filepath,body);
if(rtn.length < 13)
{
res.writeHead(201, {"Location" : rtn});
res.end();
}
else
{
res.writeHead(400, {"Content-Type" : application/json"});
res.write(''+rtn);
res.end();
}
});
}});
The problem is that the response headers are not getting updated and are always set to the default headers 200 Ok. In addition to this the server is always busy even after the response is received.
I don't think you're actually listening on a port with the code you reference.
var http = require('http');
http.createServer(function(req,res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8000);
console.log('Server running at http://127.0.0.1:8000/');
You never declare the http object as actually listening on a port/ip with the .listen() function.
Also, you don't need to wait for the req object to emit anything to respond. The function is called when the request is complete. You can listen for specific requests and route them appopriately by storing the http.Server object to a variable.
var server = http.createServer();
server.listen(8000);
server.on('request', function(req,res){ /* do something with the request */ });
More documentation on the http object can be found on the node.js documents for http

FFMPEG hangs the whole nodejs process

What I am trying to do is to make a thumbnail of a video using ffmpeg. The video data is received in a HTTP request and then piped to ffmpeg. The problem is that once the ffmpeg child process exits I simply can't send the response back.
Here is the code:
var http = require('http'),
sys = require('sys'),
child = require('child_process')
http.createServer(function (req, res) {
im = child.spawn('ffmpeg',['-i','-','-vcodec','mjpeg','-ss','00:00:03','-vframes','1','-s','100x80','./thumb/thumbnail.jpg']);
im.on('exit', function (code, signal) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"success":true}\n');
});
req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");
The problem is that calling:
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"success":true}\n');
does nothing, the client never receives the response.
After two days of debugging and googling It seems like I have found the problem.
There are two related open bugs in node.js responsible:
https://github.com/joyent/node/issues/777
https://github.com/joyent/node/issues/782
I will try to describe what I think the problem is with the 'pipe' method:
The request stream fails to invoke end on ffmpeg.stdin (probably bug #777), this causes a broken pipe error, but node.js doesn't handle the error because of bug #782, meanwhile the request stream remains paused - this blocks any response from being sent.
The hack/workaround is to resume the request stream once ffmpeg exits.
Here is the fixed code sample:
var http = require('http'),
sys = require('sys'),
child = require('child_process')
http.createServer(function (req, res) {
im = child.spawn('ffmpeg',['-i','-','-vcodec','mjpeg','-ss','00:00:03','-vframes','1','-s','100x80','./thumb/thumbnail.jpg']);
im.on('exit', function (code, signal) {
req.resume();
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('{"success":true}\n');
});
req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");
Please keep in mind that this is a hack/workaround and may lead to problems with future node.js releases once they do something about those bugs
I would try something like this.
var http = require('http'):
var sys = require('sys'):
var child = require('child_process'):
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
im = child.spawn('ffmpeg',['-i','-','-vcodec','mjpeg','-ss','00:00:03','-vframes','1','-s','100x80','./thumb/thumbnail.jpg']);
im.on('exit', function (code, signal) {
res.end('{"success":true}\n');
});
req.connection.pipe(im.stdin);
}).listen(5678, "127.0.0.1");
You are trying to pipe out data to the socket before sending the header.

Resources