为什么响应不起作用?
Why response not working?
var http = require('http');
function foo(req,res){
res.writeHead(200);
res.write('Hello,world!');
res.end('okay');
}
var app = http.createServer();
app.listen(2000);
console.log('Listening on 2000...');
您好。我现在正在学习node.js,我遇到了问题,请帮助我。
当我使用命令操作此代码时,一切都是 ok.It 返回 'Listening on 2000...'。但是当我使用浏览器时,它每次都在加载。
然后我在命令中操作 'foo' 函数它返回了我
'cannot read property writeHead'
'cannot read property write'
'cannot read property end'
您的服务器正在侦听请求,但它不知道在收到任何请求后该怎么做,因为您的 http.createServer()
函数没有被传递给回调。您定义了一个名为 foo
的回调,但您没有使用它。只需将其传递给 createServer
,您的服务器就会在您转到 localhost:2000
时响应。
var app = http.createServer(foo);
var http = require('http');
function foo(req,res){
res.writeHead(200);
res.write('Hello,world!');
res.end('okay');
}
var app = http.createServer();
app.listen(2000);
console.log('Listening on 2000...');
您好。我现在正在学习node.js,我遇到了问题,请帮助我。 当我使用命令操作此代码时,一切都是 ok.It 返回 'Listening on 2000...'。但是当我使用浏览器时,它每次都在加载。 然后我在命令中操作 'foo' 函数它返回了我 'cannot read property writeHead' 'cannot read property write' 'cannot read property end'
您的服务器正在侦听请求,但它不知道在收到任何请求后该怎么做,因为您的 http.createServer()
函数没有被传递给回调。您定义了一个名为 foo
的回调,但您没有使用它。只需将其传递给 createServer
,您的服务器就会在您转到 localhost:2000
时响应。
var app = http.createServer(foo);