我刚刚开始使用node.js并正在浏览文档.此代码甚至不运行:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
}).listen(80, "127.0.0.1");
http.Server.addListener('request', function(req,res){
console.log(req.headers);
});
console.log('Server running at http://127.0.0.1');
Run Code Online (Sandbox Code Playgroud)
我正在尝试为'request'事件添加一个侦听器到服务器对象.在文档"请求"下列为http.Server下的事件.
我从根本上误解了一些事情吗?您如何为'request'事件添加单独的侦听器功能?(也就是说,不要覆盖在createServer期间添加的那个).
它看起来像是listen不可链接的,并且您不存储您的服务器对象.尝试:
var http = require('http');
var myServer = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Node.js\n');
});
myServer.listen(80, "127.0.0.1");
myServer.addListener('request', function(req,res){
console.log(req.headers);
});
Run Code Online (Sandbox Code Playgroud)
这似乎在我的测试中起作用.
| 归档时间: |
|
| 查看次数: |
2323 次 |
| 最近记录: |