1.本地打包
本地使用以下命令打包,得到dist目录
npm run build
2.上传到服务器
将项目打包的dist以及package.json的文件上传到centos服务器
我的目录如下:
3.新建app.js文件
内容如下:
//定义目录
const fs = require('fs');
const path = require('path');
const express = require('express');
const app = express();
//vue目录
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.listen(8080);
4.在这个目录里面执行运行操作
如果install的时候报错了,上面写的
:报错如下,以及类似的,建议使用非root账户进行install
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {“os”:“darwin”,“arch”:“any”} (current: {“os”:“linux”,“arch”:“x64”})
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! yorkie@2.0.0 install: node bin/install.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the yorkie@2.0.0 install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm install
pm2 start app.js
install成功截图如下:
5. pm2相关命令
pm2 start app.js 开始进程
pm2 stop id 停止进程
pm2 delete id 删除进程
pm2 restart id 重启进程