Skip to content

mukulsaini/REST_API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API in Node.js

A simple rest api example for calculating carbon footrpints from various sources.

Authentication has been done using passport.js localapikey on every request.

 app.route('/api/authenticate')
      .post(
        passport.authenticate('localapikey', { failureRedirect: '/api/unauthorized', failureFlash: true }),
        function(req, res) {
           res.json({ message: "Authenticated" });
        });

Mocha and Chai is used to carry out the test cases.

THe app is deployed on Heroku.

Running on Browser

  • To get the emission of carbon by amount of electricity used in a country:

https://node-restapi.herokuapp.com/api/electricity?apikey=asdasjsdgfjkjhg&use=2.4&country=UK

  • To get the emission of carbon by amount of natural gas used in a country :

https://node-restapi.herokuapp.com/api/naturalGas?apikey=asdasjsdgfjkjhg&use=2.4&country=UK

Running the app locally

$ git clone https://github.com/mukulsaini/REST_API.git
$ npm install 
$ npm start

Open this link in the browser

Running Tests

$ npm test

CURL Example

$ curl --get -v -d "apikey=asdasjsdgfjkjhg" "https://node-restapi.herokuapp.com/api/electricity?use=2.4&country=UK" 
$ curl --get -v -d "apikey=asdasjsdgfjkjhg" "https://node-restapi.herokuapp.com/api/naturalGas?apikey=asdasjsdgfjkjhg&use=2.4&country=UK"