在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证书等。