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.
Related
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.
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.
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.
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);
var http = require("http");
var sys = require('sys')
var filename = process.ARGV[2];
var exec = require('child_process').exec;
var com = exec('uptime');
http.createServer(function(req,res){
res.writeHead(200,{"Content-Type": "text/plain"});
com.on("output", function (data) {
res.write(data, encoding='utf8');
});
}).listen(8000);
sys.puts('Node server running')
How do I get the data streamed to the browser ?
If you're just generally asking whats going wrong, there are two main things:
You're using child_process.exec() incorrectly
You never called res.end()
What you're looking for is something more like this:
var http = require("http");
var exec = require('child_process').exec;
http.createServer(function(req, res) {
exec('uptime', function(err, stdout, stderr) {
if (err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.end(stderr);
}
else {
res.writeHead(200,{"Content-Type": "text/plain"});
res.end(stdout);
}
});
}).listen(8000);
console.log('Node server running');
Note that this doesn't actually require 'streaming' in the sense that the word is generally used. If you had a long running process, such that you didn't want to buffer stdout in memory until it completed (or if you were sending a file to the browser, etc), then you would want to 'stream' the output. You would use child_process.spawn to start the process, immediately write the HTTP headers, then whenever a 'data' event fired on stdout you would immediately write the data to HTTP stream. On an 'exit' event you would call end on the stream to terminate it.