You are on page 1of 3

PREREQUISITES

Please make sure you have the latest stable version of node.js installed.
Download link: https://nodejs.org/en/

CREATE A PROJECT
I have already created a blank project which has the basic structure of an express application.
This project also has all the dependencies.

Repository Link: ​https://gautamakash91@bitbucket.org/gautamakash91/expresstutorial.git

If you do not have experience in git then please read the git tutorial file first.

GETTING STARTED
Once you have cloned the project open cmd or terminal. Cd into the root folder and then enter
the command ​npm install. ​This will install all the necessary dependencies.

To run the application enter the command ​node index.js ​in the terminal. This will display a
message ​Node app is running on port 8000. ​This means that your application is running.

EXPLAINING THE CODE.

If you open your browser and enter localhost:8000 then you will see WELCOME TO EXPRESS
TRAINING. This is because of the below code.

app​.​get​(​'/'​, ​function​(​req​, ​res​){


​res​.​send​(​"WELCOME TO EXPRESS TRAINING"​);
});

The application checks the path that you have entered in the browser and matches that with the
path specified in the code. For example, if you entered localhost:8000/welcome then the
application will execute the code written inside
app.get(‘/welcome’, function(req, res){
//code
});

Instead of get, you can use other methods like post, delete, etc. They will look like app.post (),
app.delete() and so on.

REQ, RES

In the below code req contains all the information that are sent to the backend and res is the
response that will be sent back to the user. You can access the parameters as below.

app​.​get​(​'/'​, ​function​(​req​, ​res​){


​console.log(req.body.parameter_name);
});

You can send any response using the res keyword as below.
app​.​get​(​'/'​, ​function​(​req​, ​res​){
​res​.​send​(​req.body.params​);
});

TESTING.
You can test your application by using postman. Select the method (GET, POST, etc), enter the
path and then click on submit. The response will be shown in the buttom of the screen.
Download Link: ​https://www.getpostman.com/
EXERCISE

Create an express application or modify this current application that will contain 3 paths. Add,
Subtract, Multiply. Each of these paths should accept 2 parameters a and b and respond with
the result.

You might also like