I am sending an api request to linkedin via the nextjs client but reached a cors access error and found I had to proxy the request via the nextjs server. I'm struggling to setup the proxy becuase I keep getting a 404 not found on the resource path (app router). I am on the latest version of nextjs
I have a route file in ./src/app/api/proxy/[...all].ts with the following content
import { NextApiRequest, NextApiResponse } from 'next';import httpProxyMiddleware from 'next-http-proxy-middleware';export default (req: NextApiRequest, res: NextApiResponse) => httpProxyMiddleware(req, res, { // Target API that we want to proxy the requests to target: 'https://api.linkedin.com', // Optionally rewrite the path. We can remove the '/api/proxy' part. pathRewrite: {'^/api/proxy/linkedin': '', // Removes /api/proxy/linkedin from the URL, leaving just /v2/me }, // Enable change of origin for the proxy changeOrigin: true, });http://localhost:3000/api/proxy/linkedin/v2/me 404 (Not Found)
, I am trying to redirect a request from my client
const response = await axios.get("/api/proxy/linkedin/v2/me", { headers: { Authorization: `Bearer ${user?.oAuthAccessToken}`, }, }); I am expecting a 200 OK or 401 Invalid access token against the linkedin api however I'm unable to reach the api endpoint as its not hitting the proxy.
, I want to map http://localhost:3000/api/proxy/linkedin/v2/me to http://linkedin/v2/me , is there something wrong in my setup?
If someone is able to point my in the right direction I'd truly be greatful, my nextjs skills are very rusty.