无法访问我在 AWS 中的 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/
更新
选择类型时,选择“自定义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}/`);
});
变化是您现在正在侦听“所有接口”,而不仅仅是本地主机。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句