package.json
{ "scripts": { "serve": "node dev-server.js" }, "devDependencies": { "chalk": "^4.1.2", "dotenv": "^16.0.3", "express": "^4.18.2", "http-proxy-middleware": "^2.0.6", "morgan": "^1.10.0", "st": "^3.0.0" }, "license": "ISC" }
dev-server.js
const express = require('express'); const morgan = require('morgan'); const st = require('st'); const { createProxyMiddleware } = require('http-proxy-middleware'); const chalk = require('chalk'); const app = express(); require('dotenv').config(); // PORT:0 可让系统自己选择可用端口 const PORT = 3000; const HOST = 'localhost'; // 控制台输出 app.use( morgan('dev', { skip: function (req, res) { return res.statusCode < 400; }, }) ); app.use( '/java-api', createProxyMiddleware({ target: process.env.API_BASE_URL, changeOrigin: true, pathRewrite: { // "^/javaApi": "", }, }) ); app.use( '/node-api', createProxyMiddleware({ target: process.env.API_BASE_URL, changeOrigin: true, pathRewrite: { // "^/api": "", }, }) ); app.use( st({ path: '.', cache: false, }) ); app.listen(PORT, HOST, () => { console.log('Starting Proxy at ' + chalk.green(`http://${HOST}:${PORT}`)); });