美文网首页
vue cli3.0+node

vue cli3.0+node

作者: Hsugar | 来源:发表于2023-02-08 15:57 被阅读0次
    • 利用node-express做中间层,解决跨域问题 引入app.js

    1.在package.json配置环境变量:
    在windows系统下 执行环境变量需要install cross-env 并在变量前赋值

    image.png
    1. ecosystem.config.js
    module.exports = {
      apps: [
        {
          name: 'PYCMS-DEV',
          script: 'app.js',
          // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
          args: '',
          instances: 1,
          autorestart: true,
          watch: false,
          max_memory_restart: '1G',
          env: {
            NODE_ENV: 'development'
          },
        },
        {
          name: 'PYCMS-TEST',
          script: 'app.js',
          // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
          args: '',
          instances: 1,
          autorestart: true,
          watch: false,
          max_memory_restart: '1G',
          env: {
            NODE_ENV: 'test'
          },
        },
        {
          name: 'PYCMS-ONLINE',
          script: 'app.js',
          // Options reference: https://pm2.io/doc/en/runtime/reference/ecosystem-file/
          args: '',
          instances: 1,
          autorestart: true,
          watch: false,
          max_memory_restart: '1G',
          env: {
            NODE_ENV: 'production'
          },
        }
      ]
    };
    
    
    1. app.js
    const fs = require('fs');
    const path = require('path');
    const history = require('connect-history-api-fallback');  //vue router history 模式
    const routes = require('./server/routes/index');
    const share = require('./server/routes/share');
    const wx = require('./server/routes/wx');
    const express = require('express');
    const cookieParser = require('cookie-parser');
    const app = express();
    app.use(cookieParser());
    
    console.log('process.env', process.env.NODE_ENV);
    
    //todo: create server config
    const config = require('./server/config');
    
    app.set('views', './server/views');
    app.set('view engine', 'ejs');
    
    app.use('/', routes);          //静态页面
    app.use('/share', share);  //分享
    app.use('/wx', wx);            //微信页面
    
    
    //vue 静态资源路径
    app.use(express.static(path.resolve(__dirname, 'server/static')));
    
    //vue router history 模式
    app.use(history({
      verbose: true,
      index: '/'
    }));
    
    //vue router history 模式错误路径统一跳转至index.html;
    app.get("/", function(req, res) {
      try {
        //跳转默认首页
        const existed = fs.existsSync(path.join(__dirname, "dist/index.html"),fs.F_OK);
        if(existed) {
          res.sendFile(path.join(__dirname, "dist/index.html"));
        } else {
          //项目编译过程中dist目录尚未生成时跳转值服务器维护提示页
          res.sendFile(path.join(__dirname, "server/static/server-error.html"));
        }
      } catch (e) {
        res.sendFile(path.join(__dirname, "server/static/server-error.html"));
      }
    });
    
    /*app.get('*', function(req, res) {
      const html = fs.readFileSync(path.resolve(__dirname, './dist/index.html'), 'utf-8')
      res.send(html)
    });*/
    
    //测试服务器部署开发环境(用于调试后端开发接口)监听4000端口,测试及线上环境监听5000端口
    app.listen(config.ports[process.env.NODE_ENV]);
    
    

    4.router-index.js

    const path = require("path");
    
    const routes = require('express').Router();
    
    /**
     * 定义跳转特殊静态页面:
     */
    //App嵌入页面
    routes.get('/adjust_rule', function (req, res) {
      res.sendFile(path.join(__dirname, "../../server/static/rule.html"));
    });
    
    
    module.exports = routes;
    
    

    在草稿箱很久了- - 发了个布 哈哈

    相关文章

      网友评论

          本文标题:vue cli3.0+node

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