- 因为 node.js 也相当于一个 http service , 可以理解位简单版的apache ,
所以先关了apache
- 永久打开 3000端口, 因为程序里用到
1 2 3 |
sudo firewall-cmd --zone=public --add-port=80/tcp --permanent sudo firewall-cmd --reload |
检查是否真的打开了
1 |
sudo firewall-cmd --list-all |
- 新建 app,js , 放哪里都可以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
const http = require('http'); const hostname = '192.168.3.116'; 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}/`); }); |
192.168.3.116 是centos 的局域网地址
- 执行此service
node app.js
- 此时即可从局域网其他机器访问 :
- 未解决问题:
a) 如何于 apache共存
好像可以在 apache设置代理 , 见
https://stackoverflow.com/questions/9831594/apache-and-node-js-on-the-same-server
b)