julian
julian

Reputation: 4714

node.js - end the request immediately?

So I have the following code -

var http = require('http');

http.createServer(function (req, res) {

console.log("Connected!");
res.writeHead(200);

req.on('data', function(data) {
    res.write(data);
});

}).listen(5000);

But when I write into chrome localhost:5000 it just load the page, and then it says that the server didn't sent any data..

I figured out that If I write req.end(); after the data event, it loads the page perfectly.
However, I don't want to end the request immediately.

What should I do?

Upvotes: 0

Views: 823

Answers (1)

Jonathan Lonowski
Jonathan Lonowski

Reputation: 123563

You'll have to call res.end() at some point, but you can wait for the req to 'end' first:

req.on('end', function () {
    res.end();
});

Upvotes: 2

Related Questions