-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmetro.config.js
More file actions
32 lines (27 loc) · 961 Bytes
/
Copy pathmetro.config.js
File metadata and controls
32 lines (27 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const { getDefaultConfig } = require("expo/metro-config");
const { createProxyMiddleware } = require("http-proxy-middleware");
const config = getDefaultConfig(__dirname);
const BACKEND_PORT = process.env.PORT || "5000";
config.server = {
...config.server,
enhanceMiddleware: (metroMiddleware) => {
const apiProxy = createProxyMiddleware({
target: `http://localhost:${BACKEND_PORT}`,
changeOrigin: false,
on: {
error: (err, req, res) => {
console.error("[Metro proxy] Backend not reachable:", err.message);
res.writeHead(502, { "Content-Type": "application/json" });
res.end(JSON.stringify({ error: "Backend server not running on port " + BACKEND_PORT }));
},
},
});
return (req, res, next) => {
if (req.url.startsWith("/api")) {
return apiProxy(req, res, next);
}
return metroMiddleware(req, res, next);
};
},
};
module.exports = config;