Skip to content

Commit

Permalink
feat: proxy 설정
Browse files Browse the repository at this point in the history
  • Loading branch information
gogumalatte committed Aug 4, 2024
1 parent b6d21cc commit ed2f8bd
Show file tree
Hide file tree
Showing 5 changed files with 1,247 additions and 69 deletions.
33 changes: 33 additions & 0 deletions api/proxy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { VercelRequest, VercelResponse } from '@vercel/node';
import http from 'http';
import https from 'https';
import url from 'url';

export default function handler(req: VercelRequest, res: VercelResponse) {
const requestUrl = req.url ?? ''; // req.url이 undefined일 경우 빈 문자열로 대체
const targetUrl = `https://doghae.site${requestUrl.replace(/^\/api/, '')}`;

const parsedUrl = url.parse(targetUrl);
const protocol = parsedUrl.protocol === 'https:' ? https : http;

const proxyReq = protocol.request(
{
hostname: parsedUrl.hostname,
port: parsedUrl.port,
path: parsedUrl.path,
method: req.method,
headers: req.headers,
},
(proxyRes) => {
res.writeHead(proxyRes.statusCode!, proxyRes.headers);
proxyRes.pipe(res, { end: true });
}
);

req.pipe(proxyReq, { end: true });

proxyReq.on('error', (err) => {
console.error('Proxy request error:', err);
res.status(500).send('Proxy request failed');
});
}
Loading

0 comments on commit ed2f8bd

Please sign in to comment.