While attempting to relay a POST request from an express backend to another backend using axios, I encountered an axios error stating "CanceledError: Request stream has been aborted". Interestingly, this issue does not arise when dealing with GET requests. The destination backend in question is implemented via NextJS.
import axios from 'axios';
import express from 'express';
import { parse } from 'url';
DESTINATION_URL = ...
async function forwardRequest(req: express.Request, res: express.Response) {
const { path } = parse(req.url, true);
const forwardUrl = DESTINATION_URL + path;
await axios(forwardUrl, {
method: req.method,
data: req,
responseType: 'stream',
}).then((response) => {
// Stream the data from the destination response to res
response.data.pipe(res);
});
}
const ForwardRouter = express.Router();
ForwardRouter.all('*', forwardRequest);
export default ForwardRouter
In an effort to resolve the issue, I experimented with modifying the axios request to mirror the headers of the request parameter of the function. However, this adjustment led to failure across all types of requests, including GET, resulting in a distinct error. It's worth mentioning that CORS settings are properly configured on the destination backend with Access-Control-Allow-Origin set to * and permitting POST requests.