美文网首页
使用pm2部署vue(node)项目

使用pm2部署vue(node)项目

作者: 氕氘氚_笔记 | 来源:发表于2020-08-29 16:41 被阅读0次

1、准备

1、配置node及环境变量
    a)windows 详细看 
     https://www.cnblogs.com/zhouyu2017/p/6485265.html
    b)Linux 详细看 
      https://www.cnblogs.com/ldld/p/7400086.html

2、验证node安装是否成功
        node -v
        npm -v

3、安装pm2、express等相关依赖, 全局安装
   npm install -g pm2 命令行全局安装pm2
   npm install -g express
    (express version 4后还需安装express-generator)
   npm install -g express-generator 
   npm install -g compression
    。。。。
4、验证是否安装成功
    pm2: pm2 -v
    express: express --version

 5、出现 bash:express:command not found 是环境变量没有配好 , 或可能是 【 终端问题 】  使用系统自带的 如cmd等等可以解决
    相关文章:    https://blog.csdn.net/weixin_30611509/article/details/95914754;
      https://blog.csdn.net/flw8840488/article/details/90513873;

2、项目打包

    只需要打包好的 dist 文件夹 

3 配置启动脚本

 项目根目录 创建 app.js ,app.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 8082'));
});

4、部署、启动管理等

在服务器创建项目的目录,pm2_test
文件夹里放 dist 和 app.js

启动: pm2 start app.js
关闭: pm2 stop all
删除: pm2 delete all
image.png

相关文章

网友评论

      本文标题:使用pm2部署vue(node)项目

      本文链接:https://www.haomeiwen.com/subject/ozcjsktx.html