5

pm2管理Node服务启动静态build打包文件

 2 years ago
source link: https://lianpf.github.io/posts/%E5%90%8E%E7%AB%AF%E5%BC%80%E5%8F%91/node%E6%9C%8D%E5%8A%A1_pm2%E5%90%AF%E5%8A%A8%E9%9D%99%E6%80%81build%E6%89%93%E5%8C%85%E6%96%87%E4%BB%B6/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

一、环境配置

centOS 安装 node & 配置全局变量

1.centos 安装 node

注意:安装和配置过程,使用baseUser用户,而不是root用户

安装&解压

// 处理上传的 xx-linux-x64.tar.xz包
[baseuser@SHTL009065205 ~]$ cd /tmp
[baseuser@SHTL009065205 tmp]$ mv node-v16.13.0-linux-x64.tar.xz ../node
[baseuser@SHTL009065205 node]$ xz -d node-v16.13.0-linux-x64.tar.xz
[baseuser@SHTL009065205 node]$ tar xf node-v16.13.0-linux-x64.tar

配置环境变量或软链

// 设置软链
[baseuser@SHTL009065205 ~]$ ln -sv /node/node-v16.13.0-linux-x64/bin/npm /usr/local/bin/
[baseuser@SHTL009065205 ~]ll /usr/local/bin/
[baseuser@SHTL009065205 ~]ln -sv /node/node-v16.13.0-linux-x64/bin/node /usr/local/bin/
[baseuser@SHTL009065205 ~]ll /usr/local/bin/

注意:最好配置环境变量,不要设置软链

// 配置环境变量
[baseuser@SHTL009065205 ~]$ cd node
[baseuser@SHTL009065205 node]$ vim /etc/profile
// 设置node&npm环境变量
[baseuser@SHTL009065205 ~]$ vim .bash_profile

.bash_profile文件末尾添加

export NODE_HOME=/node/node-v16.13.0 // NODE_HOME后面的值要添加到自己的node解压的路径
export PATH=$NODE_HOME/bin:$PATH
[baseuser@SHTL009065205 ~]$ source .bash_profile // 刷新配置文件
[baseuser@SHTL009065205 ~]$ node -v
[baseuser@SHTL009065205 ~]$ npm root -g // 查看全局package的安装位置

2.pm2的安装及命令

[baseuser@SHTL009065205 ~]$ npm install pm2 -g // 全局安装
[baseuser@SHTL009065205 ~]$ pm2 --version

常用的 pm2 命令,详见官网

$ pm2 start bin/www // bin 目录下的www入口文件
$ pm2 list // 显示所有服务
// <>符号内为可选输入 例:pm2 stop www 或者 pm2 stop 0
$ pm2 stop <app_name|id|'all'|json_conf> // 停止服务
$ pm2 restart <app_name|id|'all'|json_conf> // 重启服务
$ pm2 delete <app_name|id|'all'|json_conf> // 删除服务
$ pm2 logs // 打印日志

二、启用Node服务访问静态资源

1.express

安装 4.0以上express 要注意,必须安装express-generator

$ npm install -g express
$ npm install express-generator -g
$ express --version // 检查版本

server.js

// app.js
const express = require('express')
//创建web服务器
const app = express()
//导入gzip包
const compression = require('compression')
//文件操作
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')

//启用gzip中间件,在托管之前
app.use(compression())
//托管静态资源
app.use(express.static(path.resolve(__dirname, './dist')))

app.get('/', function(req, res) {
  const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8')
  res.send(html)
})

app.get('/home', function(req, res) {
  const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8')
  res.send(html)
})
//启动web服务器
app.listen(8888, res => {
  console.log(chalk.yellow('Start Service On 8888'))
})
$ node server.js // 或pm2 server.js

通过 localhost:8888 访问其目录下build好 /dist 静态资源

2.koa2

搭建本地服务器参考:https://blog.csdn.net/Smile_666666/article/details/107269299

$ npm install -g koa
$ koa2 --version
$ npm install koa-static -S

server.js

// app.js
const Koa = require('koa')
//创建web服务器
const app = (module.exports = new Koa())
// const chalk = require('chalk')

//托管静态资源
app.use(require('koa-static')(__dirname + '/dist'))

//启动web服务器
app.listen(8888, res => {
  //  console.log(chalk.yellow('Start Service On 8888'))
  console.log('Start Service on 8888!')
})
$ node server.js // 或pm2 server.js

通过 localhost:8888 访问其目录下build好 /dist 静态资源


最后, 希望大家早日实现:成为前端高手的伟大梦想!
欢迎交流~

微信公众号

本文版权归原作者曜灵所有!未经允许,严禁转载!对非法转载者, 原作者保留采用法律手段追究的权利!
若需转载,请联系微信公众号:连先生有猫病,可获取作者联系方式!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK