1.我在服务器根目录下创建一个app.js文件:
touch app.js
2.用vim打开:
vim app.js
3.写一个最基本的node服务:
var http = require('http')
http.createServer(function(req,res) {
res.writeHead(200, {'Content-Type': 'text/plain'})
res.end('first node')
})
.listen(8081)
console.log('your server is running at http://120.79.xxx.xx:8081/')
4.保存退出,执行:
node app.js
5.命令行打印出your server is running at http://120.79.xxx.xx:8081/
来,但是打开浏览器输入:http://120.79.xxx.xx:8081/
依然无法访问,可能防火墙未开通8081的端口号
6.我装的是CENTOS7.3,它使用firewall来代替iptables,执行以下命令查看防火墙开放的端口号:
firewall-cmd --list-ports
显示:80/tcp
,显然防火墙只开放了80端口
7.执行以下命令来开放8081端口:
firewall-cmd --zone=public --add-port=8081/tcp --permanent
然后重启防火墙:
firewall-cmd --reload
8.执行node app.js
开启node服务,打开浏览器访问120.79.xxx.xx:8081发现还是不行,不过试了下:
curl 127.0.0.1:8081
却能够成功输出信息first node
这就很费解了,查了好多资料都没找到解决办法:
清除浏览器缓存不行,谷歌,火狐,甚至ie我都试过,换浏览器都不行。
help me please!!
如果阿里云服务器的话还有个安全规则 需要配置一下端口范围