36 lines
1.0 KiB
JavaScript
36 lines
1.0 KiB
JavaScript
const http = require('http');
|
|
const httpProxy = require('http-proxy');
|
|
|
|
const proxy = httpProxy.createProxyServer({});
|
|
const app = http.createServer((req, res) => {
|
|
if (req.url.startsWith('/api/')) {
|
|
proxy.web(req, res, {
|
|
target: 'http://hs.yifenbao.cn',
|
|
changeOrigin: true,
|
|
secure: false
|
|
});
|
|
} else {
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
let fp = path.join(__dirname, '../dist/build/h5', req.url === '/' ? '/index.html' : req.url);
|
|
if (!fs.existsSync(fp)) {
|
|
fp = path.join(__dirname, '../dist/build/h5/index.html');
|
|
}
|
|
const ext = path.extname(fp);
|
|
const types = {
|
|
'.html': 'text/html',
|
|
'.js': 'application/javascript',
|
|
'.css': 'text/css',
|
|
'.png': 'image/png',
|
|
'.jpg': 'image/jpeg',
|
|
'.ttf': 'font/ttf'
|
|
};
|
|
res.writeHead(200, { 'Content-Type': types[ext] || 'text/plain' });
|
|
fs.createReadStream(fp).pipe(res);
|
|
}
|
|
});
|
|
|
|
app.listen(3000, () => {
|
|
console.log('Proxy Server running at http://localhost:3000');
|
|
});
|