如何在阿里云服务器上成功部署并测试Node.js项目? - 第二部分
最编程
2024-02-02 18:25:12
...
新建项目文件example.js
cd ~
touch example.js
修改项目文件example.js
打开example.js
vim example.js
按i进入编辑模式
将以下项目文件内容粘贴到文件中
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;
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}/`);
});
说明 :项目配置的端口号为3000。实际应用过程中,xu'yao自行配置端口号
按Esc退出编辑模式,然后输入:wq并回车以保存并关闭文件
运行项目
node ~/example.js &
运行以下命令查看是否在监听项目端口
netstat -tpln
在浏览器中输入http://地址>:端口号访问项目即可
云服务器ECS地址:阿里云·云小站