[英]My Server is up but when I try to connect to it I am getting an error [Node.js]
I am following a tutorial on Node.js and I think I have done everything to the letter, I run my server (node web.js) and tried to connect to it but I am getting an error. 我正在关注有关Node.js的教程,我想我已经完成了所有工作,运行服务器(节点web.js)并尝试连接到该服务器,但出现错误。 My code is given below, I saw an answer to a similar question but I avoided the error there, I just don't know whats wrong.
下面给出了我的代码,我看到了类似问题的答案,但是我避免了那里的错误,我只是不知道怎么了。 Please help!
请帮忙!
var http = require("http");
function process_request(req, res) {
var body = 'Thanks for calling!\n';
var content_length = body.length;
res.writeHead(200, {
'Content-Length': content_length,
'Content-Type': 'text/plain'
});
}
var s = http.createServer(process_request);
s.listen(8080);
You need to write the content that you are sending back as a response. 你需要写您发回的响应内容。 Add this after your
res.writeHead()
. 在
res.writeHead()
之后添加它。
res.write(body);
res.end();
You didn't provide the error, but you could try a 您没有提供错误信息,但可以尝试
res.send(body);
Instead. 代替。 Also confirm you're hitting http://localhost : 8080 / (assuming it's running on localhost).
还要确认您正在访问http:// localhost : 8080 /(假设它正在localhost上运行)。
You are creating content but not sending to client , using res.end you can do like 您正在创建内容,但未发送给客户端,使用res.end可以像
res.write("Hello World");
res.end(body)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.