I have two Express servers up and running - a gateway and an authentication service. I am facing an issue where the payload I set in the body of a request from my gateway to the authentication server never seems to arrive, despite there being no apparent CORS problems. Interestingly, when I try the same request using Postman, everything works fine. The code for the controller in my authentication service is as follows:
authentication-service controller
export const post = async (req: Request, res: Response): Promise<Response> => {
const newUser = req.body;
if (newUser.email === undefined || newUser.password === undefined) {
log.debug(newUser);
log.debug('Email and password were not defined');
return res.sendStatus(400);
} else {
return res.sendStatus(200);
}
}
gateway
const data = new FormInfo();
data.append('email', 'test');
data.append('password', 'test');
console.log(data);
const registerRes = await axios({
method: 'post',
url: process.env.REGISTER_USER_URL,
headers: {
...data.getHeaders(),
},
data,
})
.then((response: any) => response)
.catch((error) => error.response);
I've been troubleshooting this issue for days now, but haven't been able to identify what's going wrong. Any insights on where I might be making a mistake?
I have attempted disabling CORS.
I have also tried enabling CORS using the following approach:
const whitelist = ['http://localhost:3000'];
const corsOptions = {
credentials: true,
origin: (origin: any, callback: any) => {
if (whitelist.indexOf(origin) !== -1) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions));
The current configuration for CORS is like this:
app.use(cors());