Expressjs async router http functions



Awesome thing about expressjs when it comes to support async function is simply to have your code written this way to support async.

Noticed the async keyword after your HTTP Get request method implementation.  That's it!


router.get('/user/:id', async (req, res, next) => {
try {

const user = await getMydataFromServer({ id: req.params.id })
res.json(user);

} catch (e) {
next(e)
}
})


Comments

Popular posts from this blog

Windows SSH: Permissions for 'private-key' are too open

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20