简体   繁体   English

我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js]

[英]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:// localhost8080 /(假设它正在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.

相关问题 当我尝试在“node.js”中运行这行代码时,我收到以下错误 - When I try to run this line of code in “node.js”, I am getting the following error 为什么当我尝试访问我在 node.js 中的财产时,我变得不确定 - why I am getting undefined when I try to access on my property in node js 为什么我的本地node.js / express服务器的响应中没有得到JSON对象? - Why am I not getting a JSON object in the response from my local node.js/express server? 我收到此错误: Block is not a constructor in node.js - I am getting this error: Block is not a constructor in node.js 当我运行我的 node.js 代码时,我没有得到我想要的输出这里有什么错误? - i am not getting my desired output when i run my node.js code what is the mistake here? 我在Node.JS应用程序中使用路由,当我尝试从浏览器中打开其中一个路由时,出现错误 - I use routes in my Node.JS app and when I try to open one of the routes from the browser I get an error 我在我的 node.js 代码中收到此错误“无法读取未定义的属性 'split'”。 如何解决这个问题? - I am getting this error “Cannot read property 'split' of undefined” in my node.js code. How to resolve this? 当我向网站注册新用户时出现此错误“ secretOrPrivateKey必须具有值”! Node.js - I am having this error when I signup a new user to my web site“secretOrPrivateKey must have a value”! Node.js 我收到一个node.js module.js:473错误消息 - I am getting a node.js module.js:473 error message 我无法使用 Node.JS 服务器连接到 SQL 服务器 - I cannot connect to SQL server using Node.JS server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM