Providing an Async Handler Function to Express

Open your lib/bundle.js and add the following inside the module.exports function, after the bundle-creation API you added previously.

​ ​/**​
​ ​ * Retrieve a given bundle.​
​ ​ * curl http://<host>:<port>/api/bundle/<id>​
​ ​ */​
​ app.​get​(​'/api/bundle/:id'​, ​async​ (req, res) => {
​  ​const​ options = {
​  url: ​`​${url}​/​${req.params.id}​`​,
​  json: ​true​,
​  };
​  ​try​ {
​  ​const​ esResBody = ​await​ rp(options);
​  res.status(200).json(esResBody);
​  } ​catch​ (esResErr) {
​  res.status(esResErr.statusCode || 502).json(esResErr.error);
​  }
​ });

This code block ...

Get Node.js 8 the Right Way now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.