From 679a087e7cc648b30db4f145f8b6cca002e49b8c Mon Sep 17 00:00:00 2001 From: sin <2943460818@qq.com> Date: Sun, 10 Mar 2019 11:47:49 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=8F=90=E4=BA=A4=20jenkins=20=E6=9E=84?= =?UTF-8?q?=E5=BB=BA=E8=A7=84=E5=88=99=EF=BC=8C=E5=92=8C=20node=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- admin-web/config/server/README.md | 25 ++++++++++ .../config/server/config/chalk.config.js | 12 +++++ .../config/server/config/proxy.prod.config.js | 12 +++++ admin-web/config/server/package.json | 10 ++++ admin-web/config/server/pm2.json | 11 +++++ admin-web/config/server/server.prod.js | 35 ++++++++++++++ admin-web/config/server/shell.sh | 24 ++++++++++ admin-web/config/server/utils/proxy.build.js | 46 +++++++++++++++++++ 8 files changed, 175 insertions(+) create mode 100644 admin-web/config/server/README.md create mode 100644 admin-web/config/server/config/chalk.config.js create mode 100644 admin-web/config/server/config/proxy.prod.config.js create mode 100644 admin-web/config/server/package.json create mode 100644 admin-web/config/server/pm2.json create mode 100644 admin-web/config/server/server.prod.js create mode 100644 admin-web/config/server/shell.sh create mode 100644 admin-web/config/server/utils/proxy.build.js diff --git a/admin-web/config/server/README.md b/admin-web/config/server/README.md new file mode 100644 index 000000000..a1d0df88d --- /dev/null +++ b/admin-web/config/server/README.md @@ -0,0 +1,25 @@ + +# 使用说明 + + +### 第一步 + +``` +npm install + +``` + + +### 第二步 +复制文件到 server 目录 + +``` +cp -rf dist server +``` + +### 第三步 + +``` + node server.prod +``` + diff --git a/admin-web/config/server/config/chalk.config.js b/admin-web/config/server/config/chalk.config.js new file mode 100644 index 000000000..e6f6db99c --- /dev/null +++ b/admin-web/config/server/config/chalk.config.js @@ -0,0 +1,12 @@ +// +// 粉笔: +// 输出不同颜色的提示信息 + +const chalk = require('chalk'); + +module.exports = { + chalkError: chalk.red, + chalkSuccess: chalk.green, + chalkWarning: chalk.yellow, + chalkProcessing: chalk.blue, +}; diff --git a/admin-web/config/server/config/proxy.prod.config.js b/admin-web/config/server/config/proxy.prod.config.js new file mode 100644 index 000000000..2c162767a --- /dev/null +++ b/admin-web/config/server/config/proxy.prod.config.js @@ -0,0 +1,12 @@ +module.exports = { + '/admin-api/': { + target: 'http://180.167.213.26:18083/', + changeOrigin: true, + pathRewrite: {}, + }, + '/server/api/': { + target: 'https://preview.pro.ant.design/', + changeOrigin: true, + pathRewrite: { '^/server': '' }, + }, +}; diff --git a/admin-web/config/server/package.json b/admin-web/config/server/package.json new file mode 100644 index 000000000..7944dbaf4 --- /dev/null +++ b/admin-web/config/server/package.json @@ -0,0 +1,10 @@ +{ + "name": "browser-work-server", + "description": "node server", + "dependencies": { + "express": "^4.16.3", + "object-assign": "4.1.1", + "chalk": "2.4.1", + "http-proxy-middleware": "^0.18.0" + } +} diff --git a/admin-web/config/server/pm2.json b/admin-web/config/server/pm2.json new file mode 100644 index 000000000..407f2e5a4 --- /dev/null +++ b/admin-web/config/server/pm2.json @@ -0,0 +1,11 @@ +{ + "name": "work-web", + "script": "/app/server.prod.js", + "instances": "2", + "env": { + "NODE_ENV": "development" + }, + "env_production": { + "NODE_ENV": "production" + } +} diff --git a/admin-web/config/server/server.prod.js b/admin-web/config/server/server.prod.js new file mode 100644 index 000000000..42aaf804f --- /dev/null +++ b/admin-web/config/server/server.prod.js @@ -0,0 +1,35 @@ +const path = require('path'); +const express = require('express'); + +const { chalkSuccess } = require('./config/chalk.config'); +const proxyConfig = require('./config/proxy.prod.config'); +const proxyBuild = require('./utils/proxy.build'); + +// create app server +const app = express(); +const port = 3000; + +// host proxy +app.use(proxyBuild(proxyConfig)); + +// use index.html +app.use(express.static(path.join(__dirname, 'dist/static'))); +app.use(express.static(path.join(__dirname, 'dist'))); + +app.use((req, res) => { + res.sendFile(path.join(__dirname, 'dist', 'index.html')); +}); + +app.listen(port, error => { + if (error) { + console.error(error); + } else { + console.info( + chalkSuccess( + '==> 🌎 Listening on port %s. ' + 'Open up http://localhost:%s/ in your browser.' + ), + port, + port + ); + } +}); diff --git a/admin-web/config/server/shell.sh b/admin-web/config/server/shell.sh new file mode 100644 index 000000000..9c9274d81 --- /dev/null +++ b/admin-web/config/server/shell.sh @@ -0,0 +1,24 @@ + +# 安装依赖包 + +cd mall + +cd admin-web + +npm install + +# 开始构建 + +npm run build + +# 创建 app 运行目录 + +mkdir app + +# 复制配置文件 + +cp -rf config/server app/ + +# 复制文件到 server 努力 + +cp -rf dist app/ diff --git a/admin-web/config/server/utils/proxy.build.js b/admin-web/config/server/utils/proxy.build.js new file mode 100644 index 000000000..e250fdd5b --- /dev/null +++ b/admin-web/config/server/utils/proxy.build.js @@ -0,0 +1,46 @@ +const proxy = require('http-proxy-middleware'); +const ObjectAssign = require('object-assign'); +const { chalkError, chalkSuccess } = require('../config/chalk.config'); + +/** + * + * key : + * [0]: /api + * [1]: target url + * + * 预计写法: + * 1、 /api -> baidu.com : { } + * 2、 /user : function() { return {} } + * + * @param config + */ +module.exports = function(config) { + console.info(chalkSuccess('build proxy.%s.config 配置!'), process.env.NODE_ENV); + + const proxys = []; + + if (!config) { + console.log(chalkError('proxy.%s.config 没有配置!'), process.env.NODE_ENV); + } + + for (const key in config) { + if (/->/.test(key)) { + const keys = key.toString().split('->'); + const source = keys[0].trim(); + const target = keys[1].trim(); + + if (typeof config !== 'object') { + console.log( + chalkError('%s: proxy.%s.config 初始化失败 config 类型为 object!'), + key, + process.env.NODE_ENV + ); + break; + } + + proxys.push(proxy(source, ObjectAssign({ target }, config[key]))); + } + } + + return proxys; +};