无法访问托管在亚马逊网络服务的ubuntu EC2上的简单节点web服务器。但我看不出我错过了什么!我在AWS中提供了下面的屏幕截图-我错过了什么?请帮帮忙!非常感谢,
节点代码
const http = require('http');
const hostname = '127.0.0.1';
const port = 8080;
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}/`);
});
命令提示符
$ node index.js
命令提示符响应
Server running at http://127.0.0.1:8080/
EC2实例
安全设置
弹性IP设置
浏览器
http://"Public DNS (IPv4) value":8080/
更新
发布于 2019-03-13 06:19:09
选择类型时,选择[自定义TCP规则]:
并在端口范围字段中输入8080。
编辑
然而,这只让你走了一部分路。如果您注意到,您的服务器正在监听IP地址127.0.0.1
。这意味着它不是在倾听外部世界,而是localhost
。要在服务器机器之外访问它,您需要将代码更改为:
const http = require('http');
const hostname = '0.0.0.0';
const port = 8080;
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}/`);
});
不同的是,与只监听localhost相比,您现在监听的是“所有接口”。
https://stackoverflow.com/questions/55131339
复制相似问题