最近遇到在 windows2008部署前端項目的問題,tomact 和 ng 都不太合適小項目,而且配置比較復雜,使用 node搞個服務器吧
把打包好的靜態(tài)文件放在 dist 文件夾下,目錄如下:
-dist
--assets
-- index.html
服務器 express
const path = require('path');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const PORT = 3001;
const STATIC = path.resolve(__dirname, 'dist');
const INDEX = path.resolve(STATIC, 'index.html');
const app = express();
// Static content
app.use(express.static(STATIC));
// All GET request handled by INDEX file
app.get('*', function (req, res) {
res.sendFile(INDEX);
});
// Start server
app.listen(PORT, function () {
console.log('Server up and running on ', `http://localhost:${PORT}/`);
});
搞好服務器,后端小伙伴 cors 跨域也不會弄,只能自己代理了
代理工具 http-proxy-middleware
const path = require('path');
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const PORT = 3001;
const STATIC = path.resolve(__dirname, 'dist');
const INDEX = path.resolve(STATIC, 'index.html');
const app = express();
// Static content
app.use(express.static(STATIC));
+ app.use(
+ '/userCode', // 請求 localhost:3001/userCode/login ->
+ createProxyMiddleware({
+ target: 'http://10.1.81.45:9080/pf2/', //'http://10.1.81.45:9080/pf2//userCode/login
+ changeOrigin: true,
+ }),
+);
// All GET request handled by INDEX file
app.get('*', function (req, res) {
res.sendFile(INDEX);
});
// Start server
app.listen(PORT, function () {
console.log('Server up and running on ', `http://localhost:${PORT}/`);
});