57 lines
1.2 KiB
JavaScript
57 lines
1.2 KiB
JavaScript
//const express = require('express')
|
|
const express = require('express')
|
|
const yargs = require('yargs/yargs')
|
|
const { hideBin } = require('yargs/helpers')
|
|
|
|
const argv = yargs(hideBin(process.argv))
|
|
.option('config', {
|
|
alias: 'c',
|
|
description: 'Configuration file name (without .config.js extension)',
|
|
type: 'string',
|
|
demandOption: true
|
|
})
|
|
.help()
|
|
.argv
|
|
const configName = argv.config;
|
|
const config = require(`./${argv.config}.config.js`)
|
|
const { createProxyMiddleware } = require('http-proxy-middleware')
|
|
|
|
const app = express();
|
|
config.forEach(el => {
|
|
|
|
const [route, target, addOptions] = el;
|
|
let options = {
|
|
target,
|
|
changeOrigin: true,
|
|
timeout: 10000,
|
|
proxyTimeout: 30000,
|
|
...addOptions || {},
|
|
};
|
|
|
|
|
|
if (configName === '127.0.0.1' && route === '/api') {
|
|
options.pathRewrite = { '^/api': '' };
|
|
}
|
|
|
|
console.log('route', route, 'target', target);
|
|
|
|
app.use(
|
|
route,
|
|
createProxyMiddleware(options),
|
|
)
|
|
})
|
|
|
|
app.use(
|
|
'/',
|
|
createProxyMiddleware({
|
|
target: 'http://localhost:3333',
|
|
changeOrigin: true,
|
|
logLevel: 'debug',
|
|
timeout: 10000,
|
|
proxyTimeout: 30000,
|
|
}),
|
|
)
|
|
|
|
app.listen(4000)
|
|
console.log('proxy started on 4000')
|