O'Reilly logo

Node.js 8 the Right Way by Jim Wilson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 sets up a handler for the /bundle/:id route, which will allow us to retrieve a book bundle by its ID. Note ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required