最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - node server.js not responding - Stack Overflow

programmeradmin5浏览0评论

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.

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:

I'm fairly certain my code is right, is there anything else I'm doing wrong?

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.

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:

I'm fairly certain my code is right, is there anything else I'm doing wrong?

Share Improve this question edited Jul 24, 2015 at 18:01 Kᴏɴsᴛᴀɴᴛɪɴ Sʜɪʟᴏᴠ 13.1k17 gold badges80 silver badges77 bronze badges asked Jul 24, 2015 at 16:40 Daniel HuangDaniel Huang 811 gold badge1 silver badge6 bronze badges 1
  • how long did you wait? looks to me like there is no error there. – Coding Enthusiast Commented Jul 24, 2015 at 16:43
Add a comment  | 

5 Answers 5

Reset to default 13

The server is working just fine. You need to visit http://localhost:3000/ from your browser to view the expected output ("It's alive!").

To write messages to the console, use console.log().

The console output you show seems correct given your code.

Did you open a webbrowser and try opening 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:

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

You only need to hit the 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!");

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. Should have been response.end()

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

Change the port to 5000, and then go to http://localhost:5000/. There may be other service running on localhost:3000.

发布评论

评论列表(0)

  1. 暂无评论