我一直在研究HTTP,我遇到了node.js服务器的"data“事件。我的问题是:假设像下面这样一个非常简单的应用程序,我如何发送数据和调试流?
var http = require("http");
http.createServer(function(req, res) {
req.setEncoding("utf8");
req.on("data", function(data){
console.log("request:\n" + data);
});
}).listen(3000, "127.0.0.1");
我尝试过使用telnet和curl发送请求,但没有成功。谢谢大家!
发布于 2012-01-26 21:05:03
看起来我混淆了HTTP和TCP。如果您编写的是TCP服务器,而不是HTTP服务器:
var net = require("net");
net.createServer(function(req, res){
req.on("data", function (data) {
console.log("request:" + data);
});
}).listen(3000, "127.0.0.1");
您可以通过telnet (或netcat)轻松测试/调试到服务器的流数据:
$ telnet 127.0.0.1 3000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
> this text
> should get
> streamed to
> node.js TCP server
https://stackoverflow.com/questions/9025092
复制