Node Hero – Node.js Request Module Tutorial

RisingStack's services:


Node.js Experts

Learn more at

Sign up to our newsletter!

In this article:

This is the 6th part of the tutorial series called Node Hero – in these chapters, you can learn how to get started with Node.js and deliver software products using it.

In the following tutorial, you will learn the basics of HTTP, and how you can fetch resources from external sources using the Node.js request module.

See all chapters of the Node Hero tutorial series:

  1. Getting started with Node.js
  2. Using NPM
  3. Understanding async programming
  4. Your first Node.js HTTP server
  5. Node.js database tutorial
  6. Node.js request module tutorial [ this article ]
  7. Node.js project structure tutorial
  8. Node.js authentication using Passport.js
  9. Node.js unit testing tutorial
  10. Debugging Node.js applications
  11. Node.js Security Tutorial
  12. How to Deploy Node.js Applications
  13. Monitoring Node.js Applications

What’s HTTP?

HTTP stands for Hypertext Transfer Protocol. HTTP functions as a request–response protocol in the client–server computing model.

HTTP Status Codes

Before diving into the communication with other APIs, let’s review the HTTP status codes we may encounter during the process. They describe the outcome of our requests and are essential for error handling.

  • 1xx – Informational
  • 2xx – Success: These status codes indicate that our request was received and processed correctly. The most common success codes are 200 OK201 Created and 204 No Content.
  • 3xx – Redirection: This group shows that the client had to do an additional action to complete the request. The most common redirection codes are 301 Moved Permanently304 Not Modified.
  • 4xx – Client Error: This class of status codes is used when the request sent by the client was faulty in some way. The server response usually contains the explanation of the error. The most common client error codes are 400 Bad Request401 Unauthorized403 Forbidden404 Not Found409 Conflict.
  • 5xx – Server Error: These codes are sent when the server failed to fulfill a valid request due to some error. The cause may be a bug in the code or some temporary or permanent incapability. The most common server error codes are 500 Internal Server Error503 Service Unavailable.

If you’d like to learn more about HTTP status codes, you can find a detailed explanation about them here.

Sending Requests to External APIs

Connecting to external APIs is easy in Node. You can just require the core HTTP module and start sending requests.

Of course, there are much better ways to call an external endpoint. On NPM you can find multiple modules that can make this process easier for you. Fo​r example, the two most popular ones are the request and superagent modules.

Both of these modules have an error-first callback interface that can lead to some issues (I bet you’ve heard about Callback-Hell), but luckily we have access to the promise-wrapped versions.

Using the Node.js Request Module

Using the request-promise module is simple. After installing it from NPM, you just have to require it:

const request = require('request-promise')

Sending a GET request is as simple as:

const options = {
  method: 'GET',
  uri: ''
  .then(function (response) {
    // Request was successful, use the response object at will
  .catch(function (err) {
    // Something bad happened, handle the error

If you are calling a JSON API, you may want the request-promise to parse the response automatically. In this case, just add this to the request options:

json: true

POST requests work in a similar way:

const options = {
  method: 'POST',
  uri: '',
  body: {
    foo: 'bar'
  json: true 
    // JSON stringifies the body automatically

  .then(function (response) {
    // Handle the response
  .catch(function (err) {
    // Deal with the error

To add query string parameters you just have to add the qs property to the options object:

const options = {
  method: 'GET',
  uri: '',
  qs: {
    limit: 10,
    skip: 20,
    sort: 'asc'

This will make your request URL:

You can also define any header the same way we added the query parameters:

const options = {
  method: 'GET',
  uri: '',
  headers: {
    'User-Agent': 'Request-Promise',
    'Authorization': 'Basic QWxhZGRpbjpPcGVuU2VzYW1l'

Error handling

Error handling is an essential part of making requests to external APIs, as we can never be sure what will happen to them. Apart from our client errors the server may respond with an error or just send data in a wrong or inconsistent format. Keep these in mind when you try handling the response. Also, using catch for every request is a good way to avoid the external service crashing our server.

Putting it together

As you have already learned how to spin up a Node.js HTTP server, how to render HTML pages, and how to get data from external APIs, it is time to put them together!

In this example, we are going to create a small Express application that can render the current weather conditions based on city names.

(To get your AccuWeather API key, please visit their developer site)

const express = require('express')
const rp = require('request-promise')
const exphbs = require('express-handlebars')

const app = express()

app.engine('.hbs', exphbs({
  defaultLayout: 'main',
  extname: '.hbs',
  layoutsDir: path.join(__dirname, 'views/layouts')
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname, 'views'))

app.get('/:city', (req, res) => {
    uri: '',
    qs: {
      apiKey: 'api-key'
         // Use your accuweather API key here
    json: true
    .then((data) => {
      res.render('index', data)
    .catch((err) => {


The example above does the following:

  • creates an Express server
  • sets up the handlebars structure – for the .hbs file please refer to the Node.js HTTP tutorial
  • sends a request to the external API
    • if everything is ok, it renders the page
    • otherwise, it shows the error page and logs the error

Next up

In the next chapter of Node Hero you are going to learn how to structure your Node.js projects correctly.

In the meantime try out integrating with different API providers and if you run into problems or questions, don’t hesitate to share them in the comment section!

Share this post



Learn more at

Node.js Experts