简体   繁体   English

节点 server.js 没有响应

[英]node server.js not responding

I'm pretty new to node and I'm trying to create a simple server for my website but when I type in node server.js in command prompt nothing happens.我对节点很陌生,我正在尝试为我的网站创建一个简单的服务器,但是当我在命令提示符下输入 node server.js时没有任何反应。

var http = require("http");  
http.createServer(function(request, response) {
       response.writeHead(200, {"Content-Type": "text/plain"});
       response.write("It's alive!");
       response.end();
}).listen(3000);

This is what I see when I try to run my server.js file:这是我尝试运行server.js文件时看到的内容:

在此处输入图像描述

I'm fairly certain my code is right, is there anything else I'm doing wrong?我相当确定我的代码是正确的,还有什么我做错了吗?

The server is working just fine.服务器工作正常。 You need to visit http://localhost:3000/ from your browser to view the expected output ("It's alive!").您需要从浏览器访问http://localhost:3000/以查看预期的输出(“它活着!”)。

To write messages to the console, use console.log() .要将消息写入控制台,请使用console.log()

The console output you show seems correct given your code.鉴于您的代码,您显示的控制台输出似乎是正确的。

Did you open a webbrowser and try opening http://localhost:3000 ?您是否打开网络浏览器并尝试打开http://localhost:3000

If you want to see some console output to confirm your server started up, try adding this at the end of your server.js file:如果您想查看一些控制台输出以确认您的服务器已启动,请尝试在server.js文件的末尾添加以下内容:

console.log('Server running at http://localhost:3000');

You only need to hit the URL http://localhost:3000 .您只需要点击 URL http://localhost:3000

The server is already started after you hit the command "node server.js" and you will get the output - " It's alive! " because of this line present in your code: response.write("It's alive!");在您点击命令“node server.js”后,服务器已经启动,您将得到输出 - “ It's alive! ”,因为您的代码中存在这一行: response.write("It's alive!");

First time starting up node server made a very small mistake of not calling "response.end" as a function that made the server not response and take very long to load.第一次启动节点服务器时犯了一个非常小的错误,即没有调用“response.end”作为使服务器没有响应并且加载时间很长的函数。 Should have been response.end()应该是 response.end()

const server = http.createServer(function(request, response){
response.writeHead(200,{'Content-Type': 'text/html'})
response.write('Hello Node')
response.end 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM