vue基於node方式部署
阿新 • • 發佈:2021-02-02
在根目錄下增加檔案server.js如圖所示
server.js中內容如下:
const express = require('express')
const chalk = require('chalk')
const app = express()
const appPort = 8088
const history = require('connect-history-api-fallback')
app.use(history())
app.use(express.static('./'))
app.listen(appPort, function( ) {
console.log(
chalk.green(
chalk.underline('App listening on port: 8088')
)
)
})
port是自己設定的訪問時的引數,history是在路由中mode未history方式的處理。
然後就是執行命令即可。
npm run build
node server.js
執行完,最後在控制檯可以看到
設定代理
const express = require('express')
const chalk = require('chalk')
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express()
const appPort = 8088
const history = require('connect-history-api-fallback')
app.use(history())
app.use(express.static('./'))
app.use("/api", createProxyMiddleware({
target: "http://xxxxxx.com/",
changeOrigin: true,
pathRewrite: { '^/api': '/' }
}));
app.listen(appPort, function() {
console.log(
chalk.green(
chalk.underline('App listening on port: 8088')
)
)
})