在CentOS上設置Node.js應用程序的網絡,通常涉及以下幾個步驟:
curl -sL https://rpm.nodesource.com/setup_14.x | sudo -E bash -
sudo yum install -y nodejs
安裝完成后,可以通過以下命令驗證Node.js和npm是否安裝成功:
node -v
npm -v
/etc/sysconfig/network-scripts/
目錄下,文件名格式為ifcfg-<interface_name>
。例如,編輯ifcfg-ens33
文件來設置靜態IP地址:DEVICE=ens33
BOOTPROTO=static
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
ONBOOT=yes
修改配置文件后,需要重啟網絡服務以使更改生效:
sudo systemctl restart network
或者,對于基于NetworkManager的系統:
sudo nmcli connection down ens33 && sudo nmcli connection up ens33
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
ping www.baidu.com
const http = require('http');
const hostname = '127.0.0.1';
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}/`);
});
以上步驟是在CentOS上設置Node.js網絡的基本流程。根據具體的應用需求,可能還需要進行其他網絡相關的配置,例如設置端口轉發、配置SSL證書等。