问题概述

node.js的server启动后,hellow world可以用localhost访问到,换了IP为什么访问不了?

问题背景及描述


使用了NodeJS上的Demo代码,启动了一个webserver,用localhost或127.0.0.1能够访问到,但是换成ip地址就一直访问不到,代码如下:

const http = require('http');

const hostname = '127.0.0.1';
const port = 4000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, hostname, () => {
  console.log(`server running at http://${hostname}:${port}/`);
});


问题分析及解决过程

  • 怀疑端口问题,将阿里云里的安全组端口4000开放,仍然无法解决
  • 怀疑启动的hostname应该是用ip,然后直接将ip地址赋值给hostname,再运行,结果webserver启动失败,直接报错

成功解决方案

查看了下NodeJS的http.createServer文档,发现可以不指定hostname,然后去除hostname的声明,可以成功用localhost及ip地址访问

const http = require('http');

//const hostname = '127.0.0.1';
const port = 4000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
});

server.listen(port, () => {
  console.log(`server running at ${port}/`);
});

声明:文章属个人原创,个博地址见:
http://limuqiao.com/tech/Program-zh-nodejs-ip-customization/

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐