2013-02-13 64 views
0

所以我有下面的代码 -node.js - 立即结束请求?

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); 

但是,当我写了Chrome localhost:5000它只是加载网页,然后它说,服务器没有发送的任何数据..

我想通如果我在数据事件之后编写req.end();,它会完美地加载页面。
但是,我不想立即结束请求。

我该怎么办?

回答

2

你必须打电话res.end()在某些时候,但你可以等待reqto 'end' first

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

谢谢你,这就是我搜索了! – Israelg99 2013-02-14 03:19:33