-
Notifications
You must be signed in to change notification settings - Fork 21
/
cors-proxy.ts
33 lines (29 loc) · 901 Bytes
/
cors-proxy.ts
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
33
import express from 'express'
import {createProxyMiddleware} from 'http-proxy-middleware';
// Configuration
const REPLICATE_TOKEN = process.env.REPLICATE_API_TOKEN;
const appendAuthHeaders = (proxyReq) => {
proxyReq.setHeader('Authorization', `Token ${REPLICATE_TOKEN}`)
}
const onProxyRes = (proxyRes) => {
proxyRes.headers['Access-Control-Allow-Origin'] = '*';
proxyRes.headers['Access-Control-Allow-Headers'] = '*';
delete proxyRes.headers['content-type'];
}
const app = express();
app.use(
'/api/',
createProxyMiddleware({
router: (req) => req.originalUrl.replace(/.*https?:\/\//, 'https://'),
changeOrigin: true,
pathRewrite: {'.*' : ''},
onProxyReq: appendAuthHeaders,
onProxyRes: onProxyRes
})
);
app.listen(3000, () => {
console.log('Proxy Started')
if (process.send) {
process.send("ready");
}
});