我们测试上传一个server.js看看,先做如下准备工作:
文件上传的话我们推荐xftp或者xshell等ftp工具
我用FileZilla演示,界面都差不多的,首先我们连接到服务器:
这个就是协议不对了,我们修改下连接协议
修改成sftp协议后点击连接
如上连接成功!
既然连接成功那我们写个简单的接口:
// 导入http模块:
var http = require(‘http’);
// 创建http server,并传入回调函数:
var server = http.createServer(function (request, response) {
// 回调函数接收request和response对象,
// 获得HTTP请求的method和url:
console.log(request.method + ': ’ + request.url);
// 将HTTP响应200写入response, 同时设置Content-Type: text/html:
response.writeHead(200, {‘Content-Type’: ‘text/html’});
// 将HTTP响应的HTML内容写入response:
response.end(‘Hello world!’);
});
// 让服务器监听8080端口:
server.listen(8089);
console.log(‘Server is running at http://127.0.0.1:8089/’);
这条接口本地可以运行,那么我们再用ftp工具上传到服务器
再用ssh工具运行
运行后我们打开站点,ip:端口,发现无法访问
这时我们去检测下端口号(http://coolaf.com/tool/port)的开放情况,发现如下图,22是开放的,8089是关闭的,这也就是为什么我们ssh和ftp能通过22端口连接,8089不能访问站点的原因了,那么我们就得打开端口了,方式前面提到过,就是安全组和防火墙,开启就行当然也可以通过命令打开,下面我用命令打开端口
命令行控制防火墙
//打开8000-9000端口,成功返回success
firewall-cmd --zone=public --add-port=8000-9000/udp --permanen
//打开8089端口,成功返回success
firewall-cmd --zone=public --add-port=8089/tcp --permanent
//重启防火墙,成功返回success
firewall-cmd --reload
//检测是否打开端口,成功打开返回yes
firewall-cmd --zone=public --query-port=8089/tcp
执行了上述操作后,可以看到已经可以访问了