# Introduction

Creating REST API are more scalable, nowadays thousands of companies and organization are choosing to create the project on REST based approach for horizontal growth.

Let’s see how to create the REST API using Node.js with Express.js framework.

Here, I have integrated the Oxford Dictionary API to fetch the definition of the word which was send through API.

# Installation and Setup

  • Login through the Oxford developer portal and choose the prototype plan (free version).

3d Hover image
  • Once you choose your plan and fillup the details, you will get the activation link through your registered email. Upon activating your account you will be redirected to the login url.

3d Hover image
  • By detault your application ID and key will be generated, you can access through Menu -> API credentials or you can just click the below link.

3d Hover image

# Coding

We are going to integrate the API in Axios (Promise based HTTP client) with Expess.js framework.

To get started, install below npm packages.

Express

Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.

Axios

Axios is  a Promise based HTTP client for the browser and node.js.

Nodemon

Nodemon is a tool that helps develop node.js based applications by automatically restarting the node application when file changes in the directory are detected.

Nodemon does not require any additional changes to your code or method of development. Nodemon is a replacement wrapper for node. To use nodemon, replace the word node on the command line when executing your script.

1) Install dependency packages
npm install express --save
npm install axios --save
npm install nodemon --save-dev
2) Create axios instance

Create axios instance by configuring host URL and header parameters as follows.

const express = require("express");
const axios = require("axios");
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded())

const instance = axios.create({
  baseURL: 'https://od-api.oxforddictionaries.com',
  headers: {
    'Accept': 'application/json',
    'app_id': process.env.APPID,
    'app_key': process.env.APPKEY
  }
});

const listener = app.listen(process.env.PORT, function () {
  console.log('Your app is listening on port ' + listener.address().port);
});
3) POST: Fetching data from oxford dictionary API

Create axios instance by configuring host URL and header parameters as follows.

app.post('/search', (req, res) => {
  const lang = 'en-us';
  const input = req.body.oxford;
  try {
    instance.get(`/api/v2/entries/${lang}/${input}`)
      .then(result => {
        const data = {
          "definition": result.data.results[0].lexicalEntries[0].entries[0].senses[0].definitions[0],
          "phrases": result.data.results[0].lexicalEntries[0].phrases[0].text
        }
        res.status(200).send(data)
      })
      .catch(err => res.send(err));
  }
  catch (err) {
    console.error(err);
  }
});

In the above script, I have returned the definition and phrases from Oxford API.

4) GET: Creating index page to route HTML file

Create HTML page with search button to post the data and retrieve definition and phrases.

app.get('/', function (req, res) {
    res.sendFile('views/index.html', { root: __dirname });
});