This was a big chapter on creating RESTful APIs using Express. Let’s take a minute to reflect.
We started off with the basics—installing Express and a logging utility called Morgan. You learned how to put together the rough outline of an Express-powered web service using Node.
To manage the configuration of the service, we brought in a module called nconf. Using just three lines of code, we managed to configure the service through a config file while allowing overrides from command-line arguments and environment variables. You also learned how to use nodemon to keep your service running, automatically restarting whenever the code changes.
Next it was off to the races writing APIs that could use Elasticsearch to find books by the various fields in those documents. For this we started off using the Request module with regular callback handlers like in the previous chapter. But soon we upgraded to using Promises for managing asynchronous code flows, in particular those produced by the request-promise module.
After the search APIs, we developed APIs for working with book bundles. Building on Promises, we took advantage of async functions with the async and await keywords. This powerful combination encourages a readable, synchronous style of coding while enjoying the benefits of nonblocking, asynchronous functionality.
The followup tasks below ask you to continue filling out the suite of bundle-manipulation APIs. Take care!
In this chapter, you wrote a bunch of APIs for creating and manipulating book bundles, but not one to delete a bundle. Your task is to add one now.
Here’s the basic shell of the API you’re going to add.
| | /** |
| | * Delete a bundle entirely. |
| | * curl -X DELETE http://<host>:<port>/api/bundle/<id> |
| | */ |
| | app.delete('/api/bundle/:id', async (req, res) => { |
| | }); |
Inside the Express route handler callback function, you should do the following:
Determine the bundle’s URL based on the es config object and the request parameters.
Use await with a call to rp to suspend until the deletion is completed.
Wrap your await call in a try/catch block to handle any errors.
Hint: use the rp.delete method to send an HTTP DELETE request to Elasticsearch.
Like the previous task, this task asks you to implement a DELETE API. But this time it’s to remove a book from a bundle (not delete the bundle entirely).
Here’s the basic outline of your API:
| | /** |
| | * Remove a book from a bundle. |
| | * curl -X DELETE http://<host>:<port>/api/bundle/<id>/book/<pgid> |
| | */ |
| | app.delete('/api/bundle/:id/book/:pgid', async (req, res) => { |
| | const bundleUrl = `${url}/${req.params.id}`; |
| | |
| | try { |
| | |
| | } catch (esResErr) { |
| | res.status(esResErr.statusCode || 502).json(esResErr.error); |
| | } |
| | }); |
Inside the try{} block you’ll need to do a few things:
Note that if the bundle doesn’t contain the book whose removal is being requested, your handler should return a 409 Conflict HTTP status code. You can make this happen by throwing an object with a statusCode property set to 409 and an error object that contains information about what went wrong. This will be caught by the catch block and used to finish the Express response.
If you get stuck, check out the code download that accompanies this book. Good luck!