How to add "async" to a route with a custom middleware?

  express, javascript, middleware, node.js

I need to use await inside a route on Express, therefore it must be async. However, when I try to add async to the rute, it does not work because I use a middleware.

api.get('/users/yop', CORS(CORSOPT), async, checkauth, (req, res) => {
  await GETDATA.getUsers(db.collection('users'));
  res.json(users[req.user]);
})

Middleware looks like this:

function checkauth(req, res, next) {
 ...
 next();
}

If async and checkout are separed by commas like in the code I posted, I get this error:
await is only valid in async function

If they are not separated by commas, my code editor marks it as syntax error:
enter image description here

How to fix this?

Source: Ask Javascript Questions

LEAVE A COMMENT