Using Express.JS to consume an API

14,410

Solution 1

Express is a framework for organising your web application server. You open up certain API's routes to listen on the path and respond to the requests when necessary.

You can open API's only for internal use, i.e. calls from the browser running your app. Or you can expose your API to outer world (for example twitter API is doing that).

To connect to twitter API you need to make an outgoing request from your webserver. There are many ways to go about that, starting from native nodeJS package http https://nodejs.org/api/http.html to much more popular alternative request https://github.com/request/request

One thing to note here is that NodeJS web server are in general less restrictive than other language servers, especially when it comes to organising your app and code architecture. Hence more issues for beginners. Feel free to ask more questions.

Main purpose of app in

var app = express()

is to listen to routes (it is as well used to render pages, adding middleware etc.) and only that.

So assume u have a button on your UI which allows you to connect to twitter API. So on the click you make a GET request to your own server, to /api/twitter/connect . On your server you listen on this path as follows:

var request = require('request'); //assuming you installed this module
app.get('/api/twitter/connect', function(req, res){
  request(TWITTER_API_URL + API_KEYS, function(err, body){
      res.json(body); //res is the response object, and it passes info back to client side
  });
});

Solution 2

You can use "request" package to send requests. But in case of Cross-Origin-Request you must use "HTTPS" instead of "HTTP". You can configure Your request according to your request type like this..

 //Load the request module
var request = require('request');

//Lets configure and request
request({
    url: 'https://example.com/abc/demo', //URL to hit
    qs: {from: 'example', time: +new Date()}, //Query string data
    method: 'GET', // specify the request type
    headers: { // speciyfy the headers
        'Content-Type': 'MyContentType',
        'Custom-Header': 'Custom Value'
    },
    body: 'Hello Hello! String body!' //Set the body as a string
}, function(error, response, body){
    if(error) {
        console.log(error);
    } else {
        console.log(response.statusCode, body);
    }
});

Besides this there are others way to do the same. And for twitter you can also checkout the module called "twitter"

Share:
14,410
Onion
Author by

Onion

Updated on June 16, 2022

Comments

  • Onion
    Onion almost 2 years
    var express = require('express');
    var app = express();
    var path = require('path');
    var api = require('./api');
    
    
    app.get('/', function(req, res){
    res.sendFile(path.join(__dirname + '/index.html'));
    })
    
    
    app.listen(8080)
    console.log('Server Running');
    

    I know that we are requiring the express module. We are using the express function, we are requiring the module path and storing the reference in variable path and doing the same with api but beyond that I am a little lost. If I wanted to connect to twitter API how would I go about doing this? Can someone please explain the logic behind it so i can go learn this better and apply it by myself with different API's? I sincerely and greatly appreciate all of your help!