写一个比较典型的node服务器 server.js,可以启动vue
var express = require('express');
var path = require("path");
var http = require("http");
var bodyParser = require('body-parser');
var history = require('connect-history-api-fallback');
var proxy = require('http-proxy-middleware');
var app = express();
var port = 9090;
app.use(history({
htmlAcceptHeaders: ['text/html', 'application/xhtml+xml'],
logger: console.log.bind(console),
disableDotRule: true
}));
var proxyTable = {
'/oauth': {
target: 'http://localhost:8081',
// secure: false,
changeOrigin: true
// pathRewrite: {
// '^/api': ''
// }
},
'/core': {
target: 'http://localhost:8082',
// secure: false,
changeOrigin: true,
pathRewrite: {
'^/core': ''
}
},
'/app': {
target: 'http://localhost:8083',
// secure: false,
changeOrigin: true,
/* pathRewrite: {
'^/app': ''
} */
}
}
if(app.get('env')==='test') {
port = 8080;
proxyTable = {
'/oauth': {
target: 'http://localhost:8081',
// secure: false,
changeOrigin: true
// pathRewrite: {
// '^/api': ''
// }
},
'/core': {
target: 'http://localhost:8082',
// secure: false,
changeOrigin: true,
pathRewrite: {
'^/core': ''
}
},
'/app': {
target: 'http://localhost:8083',
// secure: false,
changeOrigin: true,
/* pathRewrite: {
'^/app': ''
} */
}
}
}
Object.keys(proxyTable).forEach(function(key){
app.use(key, proxy(proxyTable[key]));
});
app.use(bodyParser.json({
limit: '40mb',
verify: function (req, res, buf, encoding) {
req.rawBody = buf;
}
}));
app.use(bodyParser.urlencoded({
extended: true
}));
app.get('/', function (req, res) {
res.redirect("index.html");
});
app.use(express.static(path.join(__dirname, "dist")));
http.createServer(app).listen(port);
这个服务器支持vue history模式,解决了代理问题,使用环境变量启动