Coding API

Now that we have our project setup. Let's write some code. We will do this step by step so that you will be able to understand the context of it.

We will use vscode as it the most popular code editor, you can use any other editor if your choice as well.

Now, open the index.js in vscode and we will begin coding.

We will be making an api which responds back a list of tasks and users.

1. Import and initialise dependencies

Import express and cors like below

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

Line 1 & 2 are importing express and cors which we had installed earlier. Line 3 is initialising to a variable called app. The last line means the app will be using cors, cors is a express middleware, which you can learn more about here.

2. Making a route

Express has the concept of routes (which we learned in chapter 2). Since we are creating two different api endpoints which return a list of tasks and users. Let' begin making them

app.get('/users', function (req, res) {
    const users = [
        {
            id: 1,
            name: "Leanne Graham",
            email: "leanne@email.com"
        },
        {
            id: 2,
            name: "Jonh Doe",
            email: "john@email.co"
        },
        {
            id: 3,
            name: "Jane Doe",
            email: "jane123@email.hello"
        }
    ]
    res.send(users)
});

Let's break down the above code. the first line app.get('/users', function (req, res) is a route, a GET route to be specific.

The /users is the route which means when you call the api url ending with /users the below code will be executed.

A express route usually accepts two parameters the first one is used for accesing the request data and the second parameter is used for responding back, in the above code we have named as req and res.

We have variable called users which has an array of objects & we are sending it back as json to whoever is requesting the data by calling our route endpoint.

The res.send(users) method is used to send back data.

Route Explaination

3. Run API server

Okay so when ever you write a node script and you try to run it, the file is just executed only once, but since this is an api you want it to always listen, so that whenever someone call the API it will always execute and return data.

So express has way to do that by adding a listener to a port of our choice. So, adding listener in the last.

Complete code:

const express = require('express');
const cors = require('cors');
const app = express();

app.use(cors());

app.get('/users', function (req, res) {
    const users = [
        {
            id: 1,
            name: "Leanne Graham",
            email: "leanne@email.com"
        },
        {
            id: 2,
            name: "Jonh Doe",
            email: "john@email.co"
        },
        {
            id: 3,
            name: "Jane Doe",
            email: "jane123@email.hello"
        }
    ]
    res.send(users)
});

app.listen(4000, () => console.log('API running at http://localhost:4000'))

Add this at the end of your code and now you have yourself a working api.

In order to run this code, open the terminal again in the project folder and run command node index.js

This will open up the port and you see a message printed saying API running at http://localhost:4000

If you open the endpoint URL http://localhost:4000/users in your browser. You will be able to see the users lists printed in json format.

API response

Now that we made our first endpoint.

Next Chapter

Use third-party API