Consuming an API endpoint using request

Let's do a neat trick and actually consume our own endpoint as if it was some third-party external API. First, we need to ensure we have request installed and can include it in our app:

    $ npm install --save request

Next, edit server.js and make sure you include request as a required module at the start of the file:

const express = require('express'), 
    bodyParser = require('body-parser'), 
    _ = require('underscore'), 
    json = require('./movies.json'), 
    app = express(), 
    request = require('request'); 

Now, let's add a new endpoint after our existing routes, which will be an endpoint accessible in our server via a GET request to /external-api. This endpoint, however, will actually consume another endpoint on ...

Get Web Development with MongoDB and Node - Third Edition 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.