runtime backend proxy
This commit is contained in:
25
web/app/api/[...path]/route.js
Normal file
25
web/app/api/[...path]/route.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const BACKEND = process.env.BACKEND_URL || 'http://localhost:8080'
|
||||
|
||||
async function proxy(request, { params }) {
|
||||
const path = (await params).path.join('/')
|
||||
const { search } = new URL(request.url)
|
||||
const url = `${BACKEND}/api/${path}${search}`
|
||||
|
||||
const headers = new Headers(request.headers)
|
||||
headers.delete('host')
|
||||
|
||||
const init = { method: request.method, headers }
|
||||
if (!['GET', 'HEAD'].includes(request.method)) {
|
||||
init.body = request.body
|
||||
init.duplex = 'half'
|
||||
}
|
||||
|
||||
const res = await fetch(url, init)
|
||||
return new Response(res.body, {
|
||||
status: res.status,
|
||||
statusText: res.statusText,
|
||||
headers: res.headers,
|
||||
})
|
||||
}
|
||||
|
||||
export { proxy as GET, proxy as POST, proxy as PUT, proxy as DELETE, proxy as PATCH }
|
||||
@@ -1,11 +0,0 @@
|
||||
import { NextResponse } from 'next/server'
|
||||
|
||||
export function middleware(request) {
|
||||
const backendUrl = process.env.BACKEND_URL || 'http://localhost:8080'
|
||||
const { pathname, search } = request.nextUrl
|
||||
return NextResponse.rewrite(new URL(pathname + search, backendUrl))
|
||||
}
|
||||
|
||||
export const config = {
|
||||
matcher: '/api/:path*',
|
||||
}
|
||||
Reference in New Issue
Block a user