23 lines
1.3 KiB
JavaScript
23 lines
1.3 KiB
JavaScript
|
|
const { getDefaultConfig } = require('expo/metro-config');
|
||
|
|
const http = require('node:http');
|
||
|
|
const https = require('node:https');
|
||
|
|
const { environment } = require('./config/environment.cjs');
|
||
|
|
const config = getDefaultConfig(__dirname);
|
||
|
|
// Web is a developer preview only. A same-origin proxy avoids preview-browser
|
||
|
|
// private-network/CORS restrictions; installed Android calls the API directly.
|
||
|
|
config.server = { ...config.server, enhanceMiddleware: middleware => (req, res, next) => {
|
||
|
|
if (!req.url.startsWith('/native-api/')) return middleware(req, res, next);
|
||
|
|
const env = environment();
|
||
|
|
const suffix = req.url.slice('/native-api'.length);
|
||
|
|
if (suffix.includes('://') || suffix.includes('..')) { res.writeHead(400); res.end(); return; }
|
||
|
|
const target = new URL(env.apiUrl + suffix);
|
||
|
|
const transport = target.protocol === 'https:' ? https : http;
|
||
|
|
const upstream = transport.request(target, { method:req.method, headers:{...req.headers,host:target.host},timeout:220000 }, response => {
|
||
|
|
res.writeHead(response.statusCode, response.headers); response.pipe(res);
|
||
|
|
});
|
||
|
|
upstream.on('timeout',()=>upstream.destroy());
|
||
|
|
upstream.on('error',()=>{if(!res.headersSent)res.writeHead(502,{'Content-Type':'application/json'});res.end(JSON.stringify({code:502,msg:'开发预览暂未连接后端。'}));});
|
||
|
|
req.pipe(upstream);
|
||
|
|
}};
|
||
|
|
module.exports = config;
|