Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple API Calls with Node.js and Express

I'm just getting started with Node, APIs, and web applications.

I understand the basic workings of Node.js and Express, but now I want to start making calls to other service's APIs and to do stuff with their data.

Can you outline basic HTTP requests and how to grab/parse the responses in Node? I'm also interested in adding specific headers to my request (initially I'm using the http://www.getharvest.com API to crunch my time sheet data).

P.S. This seems simple, but a lot of searching didn't turn up anything that answered my question. If this is dupe, let me know and I'll delete.

Thanks!

like image 796
John Avatar asked Dec 15 '11 06:12

John


People also ask

How can I make API call from express?

Making REST API Calls In Express. Create a new route in express called /getAPIResponse. This route will make a REST API call and return the response as JSON. You'll be making use of the request client to make REST API calls in express.

How do you call an API in NodeJS?

The simplest way to call an API from NodeJS server is using the Axios library. Project Setup: Create a NodeJS project and initialize it using the following command. Module Installation: Install the required modules i.e. ExpressJS and Axios using the following command.

Can you create an API with NodeJS?

Node. js can be used to create a variety of applications, including Command-Line Applications, Web Applications, Real-time Chat Applications, REST API Servers, and so on.

Is express GOOD FOR REST API?

Express is a perfect choice for a server when it comes to creating and exposing APIs (e.g. REST API) to communicate as a client with your server application.


2 Answers

You cannot fetch stuff with Express, you should use Mikeal's request library for that specific purpose.

Installation: npm install request

The API for that library is very simple:

const request = require('request');
request('http://www.google.com', function (error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body) // Print the google web page.
  }
})

Edit: You're better of using this library instead of the http default one because it has a much nicer API and some more advanced features (it even supports cookies).

UPDATE: request has been deprecated, but there are some nice alternatives still such as 'got' or 'superagent' (look them up on npm).

like image 102
alessioalex Avatar answered Oct 19 '22 19:10

alessioalex


You can use the http client:

var http = require('http');
var client = http.createClient(3000, 'localhost');
var request = client.request('PUT', '/users/1');
request.write("stuff");
request.end();
request.on("response", function (response) {
  // handle the response
});

Also, you can set headers as described in the api documentation:

client.request(method='GET', path, [request_headers])
like image 7
Nicolas Modrzyk Avatar answered Oct 19 '22 19:10

Nicolas Modrzyk