美文网首页OpenShift Origin
openshift 使用nodejs部署 vue项目

openshift 使用nodejs部署 vue项目

作者: Herman7z | 来源:发表于2018-03-22 14:02 被阅读6次

    参考链接:
    https://www.jianshu.com/p/ce8766f3e68d
    https://github.com/sclorg/s2i-nodejs-container/blob/master/4/README.md

    由于在服务器上打包发布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 0
    
    set -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_list
    
    safeLogging () {
        if [[ $1 =~ http[s]?://.*@.*$ ]]; then
            echo $1 | sed 's/^.*@/redacted@/'
        else
            echo $1
        fi
    }
    
    shopt -s dotglob
    echo "---> 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_PROXY
    fi
    
    if [ ! -z $http_proxy ]; then
        echo "---> Setting npm http proxy to" $(safeLogging $http_proxy)
        npm config set proxy $http_proxy
    fi
    
    if [ ! -z $HTTPS_PROXY ]; then
        echo "---> Setting npm https proxy to" $(safeLogging $HTTPS_PROXY)
        npm config set https-proxy $HTTPS_PROXY
    fi
    
    if [ ! -z $https_proxy ]; then
        echo "---> Setting npm https proxy to" $(safeLogging $https_proxy)
        npm config set https-proxy $https_proxy
    fi
    
    # Change the npm registry mirror if provided
    if [ -n "$NPM_MIRROR" ]; then
        npm config set registry $NPM_MIRROR
    fi
    
    echo "---> Building your Node application from source"
    npm install express http-proxy-middleware ejs
    
    # Fix source directory permissions
    fix-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 -e
    
    if [ -e "/opt/app-root/etc/generate_container_user" ]; then
      source /opt/app-root/etc/generate_container_user
    fi
    
    # 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=5858
    fi
    
    # Set the environment to development by default.
    if [ -z "$DEV_MODE" ]; then
      export DEV_MODE=false
    fi
    
    # 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
      fi
    fi
    
    # If the official dockerhub node image is used, skip the SCL setup below
    # and just run the nodejs server
    if [ -d "/usr/src/app" ]; then
      run_node
    fi
    
    # 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 files
    app.set('views', __dirname + '/dist');                 //设置页面文件目录
    app.set('view engine', 'ejs');                          //设置模板引擎为ejs
    
    app.get('/', function (request, response) {
      response.render('index');
    });
    
    // proxy api requests
    Object.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"
      }
    

    相关文章

      网友评论

        本文标题:openshift 使用nodejs部署 vue项目

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