I am new to Node js. I am following this tutorial online. I was trying to test the code but I get an error every time about line 1 syntax error and I got the code from the tutorial so I am not sure what the problem is? Can someone help me please? Thanks in advance
//Here is the code
myfirst.js
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('Hello World!');
}).listen(8080);
var http = require('http');
//create a server object:
http.createServer(function (req, res) {
res.write('Hello World!'); //write a response to the client
res.end(); //end the response
}).listen(8080); //the server object listens on port 8080
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 made an example script from Node.js website main page like so:
var http = require('http');
http.createServer(function (req, res) {
console.log("We are connected");
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/');
Any ideas why each time I connect to 127.0.0.1:1337 (via chromium) I get 2 responses saying "We are connected"??
There are two requests by the browser. One for url / and another for /favicon.ico. Try to output the request url.
console.log(req.url);
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);
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.