部署Node.js的web服务器

一、准备

1.平台

mac或windows,这里已mac为例

2.工具

nodejs

二、安装nodejs

1.下载地址

https://nodejs.org/en/

2.安装

傻瓜式安装

三、部署服务

1.创建空目录webserver

// 创建
mkdir webserver
// 进入
cd webserver

2.安装express

sudo npm install express -g

3.创建index.js

// webserver/index.js
const express = require('express')
const app = express()

function methodSet(req, res) {
   res.set({ 
      'Access-Control-Allow-Origin': '*' // 解决跨域,允许跨到所有域名
   });
}

app.get('/', (req, res) => {
   methodSet(req, res);
   const content = 'GET OK'
   res.send(content);
});

app.post('/', function (req, res) {
   methodSet(req, res);
   res.send('POST OK');
});

app.get('/admin/', (req, res) => {
   methodSet(req, res);
   const content = 'this is admin for GET'
   res.send(content);
});

app.post('/admin/', (req, res) => {
   methodSet(req, res);
   const content = 'this is admin for POST'
   res.send(content);
});

app.put('/user', (req, res) => {
   methodSet(req, res);
   const content = 'this is user for PUT';
   res.send(content);
});

app.delete('/user/', (req, res) => {
   methodSet(req, res);
   const content = 'this is user for PUT';
   res.send(content);
});

// 添加静态文件目录(public目录下的文件将可以随意访问)
app.use(express.static('public'));
// 例:http://localhost:3000/index.html【可以请求到public/index.html】

// 添加多个静态路径:方式1
app.use('/assets/test1', express.static(path.join(__dirname, 'public', 'test1')));
app.use('/assets/test2', express.static(path.join(__dirname, 'public', 'test2')));
// 例:http://localhost:3000//assets/test1/index.html【可以请求到public/test1/index.html】

// 添加多个静态路径:方式2
app.get('/project1', (req, res) => {
   methodSet(req, res);
   res.sendFile(__dirname + '/public/project1/index.html');
});
app.get('/project2', (req, res) => {
   methodSet(req, res);
   res.sendFile(__dirname + '/public/project2/index.html');
});
// 例:http://localhost:3000/project1【可以请求到public/test1/index.html】




app.listen(3000, () => console.log('Example app listening on port 3000!'));

4.启动服务

node index.js

5.关闭服务

// 关闭服务方法1:
ctl+c

// 关闭服务方法2:
ctl+z 
// 此方法不会终止端口占用,可通过下面的方法释放端口:
1.查看指定端口PID
   lsof -i:3000
2.关闭指定PID的程序(node程序的PID)
   sudo kill -9 PID

四、用forever管理服务

1.安装forever

sudo npm install forever -g  --unsafe-perm

2.启动服务

forever start index.js

3.关闭服务

forever stop index.js

4.重启服务

forever restart index.js

5.查看服务列表

forever list

五、获取 本机ip

const os = require('os');

function getIpAddress() {
   var ifaces=os.networkInterfaces()
 
   for (var dev in ifaces) {
     let iface = ifaces[dev]
 
     for (let i = 0; i < iface.length; i++) {
       let {family, address, internal} = iface[i]
 
       if (family === 'IPv4' && address !== '127.0.0.1' && !internal) {
         return address
       }
     }
   }
 }

console.log("ip地址:", getIpAddress())

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值