一个运行Vue前端和微服务的npm包

时间:2017-10-07 21:28:17

标签: node.js webpack vue.js webpack-dev-server

我的计划是将微服务和Web应用程序部署到zeit now服务器,以便Web应用程序可以对微服务器端进行/ api调用。

问题是我无法弄清楚如何将这两个命令合并到我的package.json文件的npm脚本对象中。目前我只有2个目录,都有自己的package.json文件,微服务只是一个index.js,带有一个简单的微服务,使用micro和package.json文件来运行它。然后,我的Vue配置代理请求/ api到本地运行的微服务端口。我的配置如下。我正在使用webpack高级vuejs模板。

这可以在一个包中以部署和构建的方式进行吗?我正在使用微服务进行经过身份验证的API调用,就像你对其进行切片一样,如果将密钥存储在前端项目中,则会显示密钥,这就是为什么我选择使用只运行服务器端的微服务的原因

在我的config/index.js

dev: {
    env: require('./dev.env'),
    port: 8080,
    autoOpenBrowser: false,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    proxyTable: {
        '/api': {
           target: 'http://localhost:3000',
           changeOrigin: true,
           pathRewrite:{
                "^/api": ""
            }
        }
    },
    // CSS Sourcemaps off by default because relative paths are "buggy"
    // with this option, according to the CSS-Loader README
    // (https://github.com/webpack/css-loader#sourcemaps)
    // In our experience, they generally work as expected,
    // just be aware of this issue when enabling this option.
    cssSourceMap: false
  }

的package.json

{
  "name": "vue-project",
  "version": "0.0.1",
  "description": "",
  "author": "bdeo",
  "private": true,
  "scripts": {
    "dev": "node build/dev-server.js",
    "start": "node build/dev-server.js",
    "build": "node build/build.js",
    "unit": "cross-env BABEL_ENV=test karma start test/unit/karma.conf.js --single-run",
    "e2e": "node test/e2e/runner.js",
    "test": "npm run unit && npm run e2e",
    "lint": "eslint --ext .js,.vue src test/unit/specs test/e2e/specs"
  },
  "dependencies": {
    "axios": "^0.16.2",
    "vue": "^2.4.4",
    "vue-router": "^2.7.0",
    "vuetify": "^0.16.3"
  },
  "devDependencies": {
    "autoprefixer": "^7.1.2",
    "babel-core": "^6.22.1",
    "babel-eslint": "^7.1.1",
    "babel-loader": "^7.1.1",
    "babel-plugin-add-filehash": "^6.9.4",
    "babel-plugin-transform-runtime": "^6.22.0",
    "babel-plugin-transform-imports": "^1.4.1",
    "babel-preset-env": "^1.3.2",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-stage-2": "^6.22.0",
    "babel-register": "^6.22.0",
    "chalk": "^2.0.1",
    "connect-history-api-fallback": "^1.3.0",
    "copy-webpack-plugin": "^4.0.1",
    "css-loader": "^0.28.0",
    "cssnano": "^3.10.0",
    "eslint": "^3.19.0",
    "eslint-friendly-formatter": "^3.0.0",
    "eslint-loader": "^1.7.1",
    "eslint-plugin-html": "^3.0.0",
    "eslint-config-standard": "^6.2.1",
    "eslint-plugin-promise": "^3.4.0",
    "eslint-plugin-standard": "^2.0.1",
    "eventsource-polyfill": "^0.9.6",
    "express": "^4.14.1",
    "extract-text-webpack-plugin": "^2.0.0",
    "file-loader": "^0.11.1",
    "friendly-errors-webpack-plugin": "^1.1.3",
    "html-webpack-plugin": "^2.28.0",
    "http-proxy-middleware": "^0.17.3",
    "webpack-bundle-analyzer": "^2.2.1",
    "cross-env": "^5.0.1",
    "karma": "^1.4.1",
    "karma-coverage": "^1.1.1",
    "karma-mocha": "^1.3.0",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-phantomjs-shim": "^1.4.0",
    "karma-sinon-chai": "^1.3.1",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-spec-reporter": "0.0.31",
    "karma-webpack": "^2.0.2",
    "mocha": "^3.2.0",
    "chai": "^3.5.0",
    "sinon": "^2.1.0",
    "sinon-chai": "^2.8.0",
    "inject-loader": "^3.0.0",
    "babel-plugin-istanbul": "^4.1.1",
    "phantomjs-prebuilt": "^2.1.14",
    "chromedriver": "^2.27.2",
    "cross-spawn": "^5.0.1",
    "nightwatch": "^0.9.12",
    "selenium-server": "^3.0.1",
    "semver": "^5.3.0",
    "shelljs": "^0.7.6",
    "opn": "^5.1.0",
    "optimize-css-assets-webpack-plugin": "^2.0.0",
    "ora": "^1.2.0",
    "stylus": "^0.54.5",
    "stylus-loader": "^3.0.1",
    "rimraf": "^2.6.0",
    "url-loader": "^0.5.8",
    "vue-loader": "^13.0.4",
    "vue-style-loader": "^3.0.1",
    "vue-template-compiler": "^2.4.2",
    "webpack": "^2.6.1",
    "webpack-dev-middleware": "^1.10.0",
    "webpack-hot-middleware": "^2.18.0",
    "webpack-merge": "^4.1.0"
  },
  "engines": {
    "node": ">= 4.0.0",
    "npm": ">= 3.0.0"
  },
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not ie <= 8"
  ]
}

package.json - microservice

{
  "name": "microservice",
  "version": "0.1.0",
  "description": "",
  "author": "bdeo",
  "private": true,
  "scripts": {
    "start": "micro",
    "dev": "micro-dev"
  },
  "dependencies": {
    "micro": "latest",
    "node-fetch": "^1.7.3",
    "url": "^0.11.0"
  },
  "devDependencies": {
    "micro-dev": "^1.2.3"
  }
}

1 个答案:

答案 0 :(得分:0)

要运行这两个脚本,请参考:SO: ...run a script command from another package.json

在主package.json中,

scripts: {
  ...
  micro-dev: "cd ../path-to-micro-package-json && npm run micro-dev",
  dev-all: "npm run dev && npm run micro-dev"
}

不确定是否需要更多信息来创建组合部署到zeit?