Simple API Calls with Node.js and Express

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).


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])