From 30c2379a7c4824a4c4b84cf5b2e8d287af7cab7e Mon Sep 17 00:00:00 2001 From: sin <2943460818@qq.com> Date: Tue, 26 Mar 2019 13:18:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20server=20=E5=90=AF?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mobile-web/.gitignore | 1 + mobile-web/server/README.md | 25 ++++++++++ mobile-web/server/config/chalk.config.js | 12 +++++ mobile-web/server/config/proxy.prod.config.js | 10 ++++ mobile-web/server/package.json | 10 ++++ mobile-web/server/pm2.json | 11 +++++ mobile-web/server/server.prod.js | 36 +++++++++++++++ mobile-web/server/shell.sh | 44 ++++++++++++++++++ mobile-web/server/utils/proxy.build.js | 46 +++++++++++++++++++ 9 files changed, 195 insertions(+) create mode 100644 mobile-web/server/README.md create mode 100644 mobile-web/server/config/chalk.config.js create mode 100644 mobile-web/server/config/proxy.prod.config.js create mode 100644 mobile-web/server/package.json create mode 100644 mobile-web/server/pm2.json create mode 100644 mobile-web/server/server.prod.js create mode 100644 mobile-web/server/shell.sh create mode 100644 mobile-web/server/utils/proxy.build.js diff --git a/mobile-web/.gitignore b/mobile-web/.gitignore index 185e66319..7c24a76a0 100644 --- a/mobile-web/.gitignore +++ b/mobile-web/.gitignore @@ -1,6 +1,7 @@ .DS_Store node_modules /dist +dist # local env files .env.local diff --git a/mobile-web/server/README.md b/mobile-web/server/README.md new file mode 100644 index 000000000..a1d0df88d --- /dev/null +++ b/mobile-web/server/README.md @@ -0,0 +1,25 @@ + +# 使用说明 + + +### 第一步 + +``` +npm install + +``` + + +### 第二步 +复制文件到 server 目录 + +``` +cp -rf dist server +``` + +### 第三步 + +``` + node server.prod +``` + diff --git a/mobile-web/server/config/chalk.config.js b/mobile-web/server/config/chalk.config.js new file mode 100644 index 000000000..e6f6db99c --- /dev/null +++ b/mobile-web/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/mobile-web/server/config/proxy.prod.config.js b/mobile-web/server/config/proxy.prod.config.js new file mode 100644 index 000000000..a3ce7f6f4 --- /dev/null +++ b/mobile-web/server/config/proxy.prod.config.js @@ -0,0 +1,10 @@ +module.exports = { + '/admin-api -> http://180.167.213.26:18083/': { + changeOrigin: true, + pathRewrite: {}, + }, + '/server/api -> https://preview.pro.ant.design/': { + changeOrigin: true, + pathRewrite: { '^/server': '' }, + }, +}; diff --git a/mobile-web/server/package.json b/mobile-web/server/package.json new file mode 100644 index 000000000..7944dbaf4 --- /dev/null +++ b/mobile-web/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/mobile-web/server/pm2.json b/mobile-web/server/pm2.json new file mode 100644 index 000000000..5dec00dbc --- /dev/null +++ b/mobile-web/server/pm2.json @@ -0,0 +1,11 @@ +{ + "name": "admin-web", + "script": "./server.prod.js", + "instances": "2", + "env": { + "NODE_ENV": "development" + }, + "env_production": { + "NODE_ENV": "production" + } +} diff --git a/mobile-web/server/server.prod.js b/mobile-web/server/server.prod.js new file mode 100644 index 000000000..b7c8acde0 --- /dev/null +++ b/mobile-web/server/server.prod.js @@ -0,0 +1,36 @@ +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/css'))); +app.use(express.static(path.join(__dirname, 'dist/js'))); +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/mobile-web/server/shell.sh b/mobile-web/server/shell.sh new file mode 100644 index 000000000..5dc3deca7 --- /dev/null +++ b/mobile-web/server/shell.sh @@ -0,0 +1,44 @@ + +# 安装依赖包 + +cd admin-web + +cnpm install + +# 开始构建 + +#npm run build + +# 创建 app 运行目录 + +rm -rf app + +mkdir app + +# 复制配置文件 + +cp -rf config/server app/ + +# npm 构建项目 + +npm run build:admin-web + +# 复制文件到 server 努力 + +cp -rf dist app/server + +# 复制文件到 project 目录执行 + +if [ ! -d "/work2/project/admin-web/server/dist" ]; then + rm -rf /work2/project/admin-web/server/dist +fi + +cp -rf app/server /work2/project/admin-web + +# 安装 server 依赖项 +cd /work2/project/admin-web/server + +cnpm install + +# 启动服务 +pm2 restart pm2.json diff --git a/mobile-web/server/utils/proxy.build.js b/mobile-web/server/utils/proxy.build.js new file mode 100644 index 000000000..e250fdd5b --- /dev/null +++ b/mobile-web/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; +};