Next.js Discord

Discord Forum

next13 app directory next-http-proxy-middleware

Answered
Bombay posted this in #help-forum
Open in Discord
BombayOP
import { NextApiRequest, NextApiResponse } from 'next';
import httpProxyMiddleware from 'next-http-proxy-middleware';
import { config } from 'dotenv';
config();

const domain = process.env.domain;

const api = async (req: NextApiRequest, res: NextApiResponse) => {
console.log('api called');
// You can use the http-proxy option
return httpProxyMiddleware(req, res, {
target: ${domain},
// In addition, you can use the pathRewrite option provided by next-http-proxy
pathRewrite: [
{ patternStr: '^/api/backend', replaceStr: '/' }
],
});
};

export const POST = async (req: NextApiRequest, res: NextApiResponse) => {
return api(req, res);
};
// Separate functions for handling GET and POST requests
export const GET = async (req: NextApiRequest, res: NextApiResponse) => {
return api(req, res);
};

error TypeError: Cannot set property url of #<NextRequest> which has only a getter
Answered by European sprat
https://www.npmjs.com/package/next-http-proxy-middleware

At the very top, read the "before using" section. it looks like you should be using the built in rewrites
View full answer

10 Replies

European sprat
https://www.npmjs.com/package/next-http-proxy-middleware

At the very top, read the "before using" section. it looks like you should be using the built in rewrites
Answer
BombayOP
does it have support to write headers? i want to set accesstoken from nextauth . i dont think i could do that in config file
European sprat
you can set headers in middleware
BombayOP
thanks bro
BombayOP
i have an endpoint /webhook where paypal sends me data in headers and stufff. if i use config rewrites will all the data get passed to the backend server
or middleware for that
European sprat
API requests will go through middleware too
but there is an order of operations to things. you'd have to look at the docs but rewrites might happen before middleware (probably does)