-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy-server.js
More file actions
43 lines (39 loc) · 1.57 KB
/
proxy-server.js
File metadata and controls
43 lines (39 loc) · 1.57 KB
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
34
35
36
37
38
39
40
41
42
43
const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
// リクエスト内容をダンプするミドルウェア
app.use((req, res, next) => {
console.log('--- Incoming Request ---');
console.log('Method:', req.method);
console.log('URL:', req.originalUrl);
console.log('Headers:', req.headers);
console.log('Body:', req.body); // もしリクエストにボディが含まれている場合
console.log('------------------------');
next();
});
// 必要なヘッダーをセットしてプロキシ
app.use(
'/',
createProxyMiddleware({
target: process.env.PROXY_URL,
changeOrigin: true,
onProxyReq: (proxyReq, req, res) => {
// 必要なヘッダーを追加
proxyReq.setHeader('X-Real-IP', req.connection.remoteAddress);
proxyReq.setHeader('X-Forwarded-For', req.headers['x-forwarded-for'] || req.connection.remoteAddress);
proxyReq.setHeader('X-Forwarded-Proto', req.protocol);
proxyReq.setHeader('Authorization', req.headers['authorization'] || ''); // Authorization ヘッダも追加
},
onProxyRes: (proxyRes, req, res) => {
// プロキシ先からのレスポンスヘッダーを表示する
console.log('--- Proxy Response ---');
console.log('Status:', proxyRes.statusCode);
console.log('Headers:', proxyRes.headers);
console.log('------------------------');
}
})
);
const port = Number(process.env.PORT || '5000');
app.listen(port, () => {
console.log(`Proxy server running on http://localhost:${port}`);
});