简体   繁体   English

node.js 8080端口未监听

[英]node.js 8080 port isn't listening

I'm following the node.js tutorial in here, 我在这里关注node.js教程,

http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/ http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

this is the code, 这是代码,

var sys = require("sys"),  
http = require("http");  

 http.createServer(function(request, response) {  
response.sendHeader(200, {"Content-Type": "text/html"});  
response.write("Hello World!");  
response.close();  
 }).listen(8080);  

 sys.puts("Server running at http://localhost:8080/");  

in here, it says run like this url, 在这里,它说像这样的网址运行,

server's ip:8080/ 服务器的IP:8080 /

but if i do this, 但是如果我这样做,

it just shows, cannot connect to this url. 它只是显示,无法连接到该网址。

i opened 8080 port in the server. 我在服务器上打开了8080端口。

=========================== ==========================

I'm assuming something is screwed up with codeigniter url helper... 我假设Codeigniter网址帮助程序搞砸了...

The tutorial may be using an incorrect or deprecated method. 本教程可能使用了不正确或不建议使用的方法。 Replace 更换

response.sendHeader(200, {"Content-Type": "text/html"}); 

with

response.writeHead(200, {"Content-Type": "text/html"}); 

and

response.close();

with

response.end();

I Agree with the answer of Third .... make those changes and if it is local use this URL 我同意“第三……”的回答,进行更改,如果是本地使用此URL

http://127.0.0.1:8080/

But

If you are running your server not on localmachine but on something like webserver(AWS) , You have to let the security of AWS firewall to allow the port to be public on the internet and also remember to use the AWS instance URL 如果您不是在本地计算机上运行服务器,而是在诸如webserver(AWS)类的webserver(AWS)上运行服务器,则必须让AWS防火墙的安全性允许该端口在Internet上公开,并且还要记住使用AWS实例URL

http://AWSinstanceURL:portno/

Use this one 用这个

//Lets require/import the HTTP module
var http = require('http');

//Lets define a port we want to listen to
const PORT=8080; 

//We need a function which handles requests and send response
function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}

//Create a server
var server = http.createServer(handleRequest);

//Lets start our server
server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});

Now open http://localhost:8080 and u will get your result. 现在打开http:// localhost:8080 ,您会得到结果。

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

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