-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6d21cc
commit ed2f8bd
Showing
5 changed files
with
1,247 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
} |
Oops, something went wrong.