I have a Node.js service that makes two AJAX calls to a backend server. The problem I’m facing is that the service takes more than 40 seconds to complete when accessed through an AJAX call from the frontend, but the same backend service responds within 5 seconds when tested directly using Postman. Below is the code for the Node.js service:
// ...
const PORT = process.env.PORT || 5001;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(PORT, () => {
console.log(`Server listening on ${PORT}`, new Date());
});
app.get('/api', (req, response, next) => {
console.log(`[${new Date()}] API called...`);
let user = md5(req.query.user+'mytest').substring(0,5)
let secretcode = req.query.secretcode
let type = req.query.type
console.log(`[${new Date()}] User: ${user}, Secret Code: ${secretcode}, Type: ${type}`);
if(user == secretcode) {
console.log(`[${new Date()}] Initiating checkApiUser request...`);
axios.post('http://192.168.xxx.xxx:xxxx/checkUser', qs.stringify({
acUsername: req.query.user,
acPassword: secretcode
})).then(res => {
console.log(`[${new Date()}] checkApiUser request completed.`);
console.log(`[${new Date()}] Initiating apiCatalog request...`);
axios.post('http://192.168.xxx.xxx:xxxx/catalog', qs.stringify({
data1: res.data.data1,
data2: res.data.data2,
data3: req.query.data3 ? req.query.data3 : 0,
data4: req.query.data4 && req.query.data4 == "some" || req.query.data4 == "other" ? 0 : 1,
data5: req.query.data5 ? req.query.data5 : "",
//...
})).then(resp => {
console.log(`[${new Date()}] apiCatalog request completed.`);
let products = resp.data.product;
if(type == "xml") {
response.set('Content-Type', 'text/xml');
return response.send(o2x({
'?xml version="1.0" encoding="utf-8"?' : null,
catalog: {
product: products
}
}));
} else {
return response.json({catalog: products})
}
}).catch(err => {
console.log(err)
})
}).catch(err => {
console.log(err)
});
}
});
Here are the logs:
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] API called...
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] User: sample, Secret Code: 34sdg, Type: undefined
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] Initiating checkApiUser request...
[Wed May 29 2024 14:02:47 GMT+0200 (Central European Summer Time)] checkApiUser request completed.
[Wed May 29 2024 14:02:48 GMT+0200 (Central European Summer Time)] Initiating apiCatalog request...
[Wed May 29 2024 14:03:34 GMT+0200 (Central European Summer Time)] apiCatalog request completed.
What could be causing this significant delay in the AJAX call from the frontend compared to using Postman? Any insights or suggestions on how to troubleshoot this would be greatly appreciated.