javascriptnode.jsexpresshttprequest

How to make an HTTP GET request in Node.js Express?


How can I make an HTTP request from within Node.js or Express.js? I need to connect to another service. I am hoping the call is asynchronous and that the callback contains the remote server's response.


Solution

  • Here is a snippet of some code from a sample of mine. It's asynchronous and returns a JSON object. It can do any form of GET request.

    Note that there are more optimal ways (just a sample) - for example, instead of concatenating the chunks you put into an array and join it etc... Hopefully, it gets you started in the right direction:

    const http = require('http');
    const https = require('https');
    
    /**
     * getJSON:  RESTful GET request returning JSON object(s)
     * @param options: http options object
     * @param callback: callback to pass the results JSON object(s) back
     */
    
    module.exports.getJSON = (options, onResult) => {
      console.log('rest::getJSON');
      const port = options.port == 443 ? https : http;
    
      let output = '';
    
      const req = port.request(options, (res) => {
        console.log(`${options.host} : ${res.statusCode}`);
        res.setEncoding('utf8');
    
        res.on('data', (chunk) => {
          output += chunk;
        });
    
        res.on('end', () => {
          let obj = JSON.parse(output);
    
          onResult(res.statusCode, obj);
        });
      });
    
      req.on('error', (err) => {
        // res.send('error: ' + err.message);
      });
    
      req.end();
    };
    
    

    It's called by creating an options object like:

    const options = {
      host: 'somesite.com',
      port: 443,
      path: '/some/path',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    };
    

    And providing a callback function.

    For example, in a service, I require the REST module above and then do this:

    rest.getJSON(options, (statusCode, result) => {
      // I could work with the resulting HTML/JSON here. I could also just return it
      console.log(`onResult: (${statusCode})\n\n${JSON.stringify(result)}`);
    
      res.statusCode = statusCode;
    
      res.send(result);
    });
    

    UPDATE

    If you're looking for async/await (linear, no callback), promises, compile time support and intellisense, we created a lightweight HTTP and REST client that fits that bill:

    Microsoft typed-rest-client