So far, my API-middleware code is functioning properly. I am being cautious to avoid any blocking calls in my express server, which is why I have implemented a function containing asynchronous fetch calls.
I'm wondering if this extra step is necessary at all. Wouldn't the first await statement already unblock my express server?
Below is the code snippet:
var express = require('express')
var router = express.Router()
const asyncMiddleware = require('./utils/asyncMiddleware');
async function fetchDataFromAPI(URL, bodyJson, wpToken) {
try {
return await fetch(URL, {
method: "POST",
credentials: "same-origin",
headers: {
"Authorization": "Bearer " + wpToken,
"Content-Type": "application/json",
"Accept": "application/json"
},
body: bodyJson
});
} catch (error) {
return {status:544, error:error};
}
}
router.post("/registerVendor", asyncMiddleware(async (req, res, next) => {
const response = await fetchDataFromAPI(myApiUrl, req.body, 1)
return res
.status(response.status)
.send({ data: response});
}));
module.exports = router