9

Wondering how I can make a GET request to a JSON API using Node.js. I preferably want to use Express however it is not necessary, and for the output to be on a Jade page. I'm still completely new to Node.js and backend languages as a whole.

3 Answers 3

17
var request = require('request');
request('<API Call>', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var info = JSON.parse(body)
    }
})

This will make an HTTP request to the API and upon success parse the response into JSON.

As far as getting the response onto a Jade page do you wish to do an API call (to your own server) and then use AngularJS/ jQuery/ another framework to fill in the information?

If you wish to add this to your own route consider embedding it like such:

var express = require('express');
var cors = require('cors');
var request = require('request');
var app = express();
app.use(express.bodyParser());
app.use(cors());
app.get('<Your Route>', function(req, res){
  request('<API Call>', function (error, response, body) {
    if (!error && response.statusCode == 200) {
      var info = JSON.parse(body)
      // do more stuff
      res.send(info);
    }
  })
});
app.listen(3000);
console.log("The server is now running on port 3000.");
Sign up to request clarification or add additional context in comments.

4 Comments

Does this code go in 'app.js' or does it go in one of the js files in the 'views' folder? Also I do intend to use another framework to fill the information on a page - do you know how to do this?
@frumpygrumpy I would consider putting it in app.js, but it depends how you are structuring your program. In most of my projects I leave my Jade files to be in the 'views' folder. It depends which framework you are wishing to use, but you will need to do some form of a JSON/HTTP GET to receive this information for your front end.
I'm using Express at the moment so my Jade files are in the 'views' folder. I want to use the API to allow users to use a search option so would it be wise to put this request in app.js or in 'index.js' in routes?
You should put this in routes.
7

I like to use the request package:

npm install --save request

And the code:

var request = require('request');

request({url: 'http://yourapi.com/', json: true}, function(err, res, json) {
  if (err) {
    throw err;
  }
  console.log(json);
});

2 Comments

nice catch in the parser json true
Shouldn't that be var request = require('request'); instead of calling request like a function to initialize it?
2

Also, the same people who brought you the request package, have come out with a promise based version backed by bluebird called, not surprisingly, request-promise:

request-promise npm page

Some folks also prefer super agent, which allows you to chain commands:

superagent npm page

Here's an example from their docs:

request
  .post('http://localhost:3000/api/pet')
  .send({ name: 'Manny', species: 'cat' })
  .set('X-API-Key', 'foobar')
  .set('Accept', 'application/json')
  .end(function(err, res){
    // Calling the end function will send the request 
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.