In the technology stack for my project, I am using a Next.js
server for the front end and a separate back-end server to handle user data storage.
When a client needs to send a request to the back end, they first make an API request to the Next.js
server, which then communicates with the back-end server to retrieve the necessary data.
The challenge I am facing is effectively forwarding these requests from the client to the server and handling the response without losing any data or creating messy code.
Currently, this is the approach I have implemented:
export default function handler(req: NextApiRequest, res: NextApiResponse){
fetch(url, {
method: "post",
body: req.body
}).then(response => res.status(200).json(response.body))
}
However, one major issue with this solution is that it does not include the necessary headers. While I could manually add specific header fields, this approach is not scalable as there are numerous other fields to consider. I am looking for a more comprehensive way to ensure that all required headers are included.
Important Note: The reason I need to intercept the request is to insert a JWT token for authentication purposes.