由于在服务器上打包发布vue项目很慢,也很消耗服务器资源,所有把这个过程放到本地来进行,本地打包好项目后提交到git. openshift build 下载源代码后直接使用打包好的文件 启动 express服务器
1. 覆盖openshift 已经有的s2i 中nodejs 的脚本代码
在根目录下面创建文件夹 .s2i\bin
,在 .s2i\bin
创建两个文件 assemble
run
assemble
文件内容:
#!/bin/bash
# Prevent running assemble in builders different than official STI image.# The official nodejs:6-onbuild already run npm install and use different# application folder.[ -d "/usr/src/app" ] && exit 0set -e# FIXME: Linking of global modules is disabled for now as it causes npm failures# under RHEL7# Global modules good to have# npmgl=$(grep "^\s*[^#\s]" ../etc/npm_global_module_list | sort -u)# Available global modules; only match top-level npm packages#global_modules=$(npm ls -g 2> /dev/null | perl -ne 'print "$1\n" if /^\S+\s(\S+)\@[\d\.-]+/' | sort -u)# List all modules in common#module_list=$(/usr/bin/comm -12 <(echo "${global_modules}") | tr '\n' ' ')# Link the modules#npm link $module_listsafeLogging () { if [[ $1 =~ http[s]?://.*@.*$ ]]; then echo $1 | sed 's/^.*@/redacted@/' else echo $1 fi}shopt -s dotglobecho "---> Installing application source ..."mv /tmp/src/* ./if [ ! -z $HTTP_PROXY ]; then echo "---> Setting npm http proxy to" $(safeLogging $HTTP_PROXY) npm config set proxy $HTTP_PROXYfiif [ ! -z $http_proxy ]; then echo "---> Setting npm http proxy to" $(safeLogging $http_proxy) npm config set proxy $http_proxyfiif [ ! -z $HTTPS_PROXY ]; then echo "---> Setting npm https proxy to" $(safeLogging $HTTPS_PROXY) npm config set https-proxy $HTTPS_PROXYfiif [ ! -z $https_proxy ]; then echo "---> Setting npm https proxy to" $(safeLogging $https_proxy) npm config set https-proxy $https_proxyfi# Change the npm registry mirror if providedif [ -n "$NPM_MIRROR" ]; then npm config set registry $NPM_MIRRORfiecho "---> Building your Node application from source"npm install express http-proxy-middleware ejs# Fix source directory permissionsfix-permissions ./
run
文件内容
#!/bin/bash
# S2I run script for the 'nodejs' image.# The run script executes the server that runs your application.## For more information see the documentation:# https://github.com/openshift/source-to-image/blob/master/docs/builder_image.md#set -eif [ -e "/opt/app-root/etc/generate_container_user" ]; then source /opt/app-root/etc/generate_container_userfi# Runs the nodejs application server. If the container is run in development mode,# hot deploy and debugging are enabled.run_node() { echo -e "Environment: \n\tDEV_MODE=${DEV_MODE}\n\tNODE_ENV=${NODE_ENV}\n\tDEBUG_PORT=${DEBUG_PORT}" if [ "$DEV_MODE" == true ]; then echo "Launching via nodemon..." exec nodemon --debug="$DEBUG_PORT" else echo "Launching via npm..." exec npm run -d $NPM_RUN fi}#Set the debug port to 5858 by default.if [ -z "$DEBUG_PORT" ]; then export DEBUG_PORT=5858fi# Set the environment to development by default.if [ -z "$DEV_MODE" ]; then export DEV_MODE=falsefi# If NODE_ENV is not set by the user, then NODE_ENV is determined by whether# the container is run in development mode.if [ -z "$NODE_ENV" ]; then if [ "$DEV_MODE" == true ]; then export NODE_ENV=development else export NODE_ENV=production fifi# If the official dockerhub node image is used, skip the SCL setup below# and just run the nodejs serverif [ -d "/usr/src/app" ]; then run_nodefi# Allow users to inspect/debug the builder image itself, by using:# $ docker run -i -t openshift/centos-nodejs-builder --debug#[ "$1" == "--debug" ] && exec /bin/bash run_node
2. 配置vue 项目服务器接口代理
module.exports = { dev: { // Paths assetsSubDirectory: 'static', assetsPublicPath: '/', proxyTable: { '/server': { target: 'http://health-server.shiwaixiangcun.cn', changeOrigin: true } }, } ..... }
3. 配置启动express服务器的文件
在根路径下面创建server.js
文件
var express = require('express');
var app = express();
var proxyMiddleware = require('http-proxy-middleware')
var config = require('./config')
var proxyTable = config.dev.proxyTable
app.set('port', (process.env.PORT || 8080)); //设置端口app.use(express.static(__dirname + '/dist',{maxAge: 24*60*60*1000})); //设置静态文件目录// views is directory for all template filesapp.set('views', __dirname + '/dist'); //设置页面文件目录app.set('view engine', 'ejs'); //设置模板引擎为ejsapp.get('/', function (request, response) { response.render('index'); });// proxy api requestsObject.keys(proxyTable).forEach(function (context) { var options = proxyTable[context] if (typeof options === 'string') { options = {target: options} } app.use(proxyMiddleware(options.filter || context, options)) }); app.get('*', function (request, response) { //404 response.render('index'); }); app.listen(app.get('port'), function () { //启动服务器,监听上面设置的端口 console.log('Node app is running on port', app.get('port')); });
4. package.json
添加script
"scripts": { "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js", "start": "node server.js", "build": "cnpm install && node build/build.js" }
作者:9527华安
链接:https://www.jianshu.com/p/27634d77b941