Hello everyone,

Today I’m going to teach you guys some express.js api.

Let’s get started 🙂

First create a sample folder called “myAPI” or any other name you prefer. then open console from that location by shift+right and selecting “Open command window here”

Untitled.png

or you can simple open cmd by pressing ctrl + r and then type cmd on it and then cd > in to you folder location

After that there are some installation that we need to do. To work with this api we need to install npm and body parser in to our workspace to do that type in you cmd

npm init

this will create package.json in your folder location (open your created folder and see )

Then type npm install express –save on your console

npm install express --save

after that install body-parser dependency by typing npm install body-parser –save on your cmd

npm install body-parser --save

All the pre steps that we need to create a simple api is done right now. Lets begin the coding 🙂

Lets first try to serve HTML page via expressJS. That means we are going to pass an index.html (simple webpage) via a port.

First create a simple index.html file

<html>

<body>

            <h1> THIS IS MY FIRST EXPRESS JS SERVER </h1>

</body>

</html>

then save following code as server.js in you location

'use strict';

const express = require('express');

const app = express();

app.use(express.static(__dirname));

app.get('/', (req, res, next) => {
 res.sendFile('index.html');
});

app.listen(3000, err => {
 if (err) {
 console.error(err);
 return;
 }
 console.log('app listening on port 3000');
});

 

Now open cmd in your folder location and type “node server.js” and see whats happening. You will see “app listening on port 3000” on your console. Now open your web-browser and type localhost:3000 and see whats in there 🙂 you will see a webpage saying “ THIS IS MY FIRST EXPRESS JS SERVER “. (or what ever you placed in your .html file)

Congratulations you just served a html via expressJS 🙂

Now lets try to do some advance stuff. Lets implement a post method in our api.

Modify your sever.js to this.

 

'use strict';

const bodyParser = require('body-parser'),
 express = require('express');

const app = express();

app.use(bodyParser.json());

app.use(express.static(__dirname));

app.get('/', (req, res, next) => {
 res.sendFile('index.html');
});

const users = [];

app.post('/users', (req, res, next) => {
 const user = req.body;
 users.push(user);
 res.json(user);
});

app.listen(3000, err => {
 if (err) {
 console.error(err);
 return;
 }
 console.log('app listening on port 3000');
});

 

In the above mention code we have implemented a post request which is in localhost:3000/users route which accept a json object and push it in to another json array and send the resulting array in the response body. body-parser is used to store data which is in the request body of a request call. Lets check our code. As previous type server.js on your console and startup the server. To call our api we can use other application like postman , soapui and manymore. Lets use postman as our api call middleman. Go to this link and install postman and launch it. (Check my previous blogpost to get an idea about how postman operates.) In postman set method to post and set address as localhost:3000/users and set body to raw and type to JSON  then type a sample json in the method body

{

"name":"Chamindu",

"age":"22"

}

 

Then click send button and see the output. there will be a json as an output which has our given details 🙂 . This is how expressJS api works. you can do so many stuffs using this kind of application you can call this from you own webpage even connect your api with databases and all. Ill share how to use PUT and DELETE method in your api in next blog post and will also share how to use mongo db along with expressJS api. till then good bye 🙂

 

Brought to you by Yinza’s Class Room

 

 

 

 

 

Advertisements