# Day 17 of 100days of code.

### Fundamentals of nodeJS

## Getting started with ExpressJS

Express JS is a free open source web application framework for node JS. It simplifies node JS code with simple one or two lines of codes. Express is completely built on node JS.

### Generate package.json

Lets install the express framework in our application. Firstly lets generate our `package.json` file by running the command: `npm init.` We get a bunch of questions, answer accordingly. The `package.json` file after configuration👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707129743624/8b4dd728-859e-4d01-a8b8-b044902931db.png align="center")

To install express run the command: `npm i express`. We are installing the express as a regular dependency for our application. After the installation, a folder named `node_modules` will be automatically added to the project. This folder contains all the dependencies of our application.👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707130032656/9ccf7835-3334-4f21-a73e-0bdc634a5566.png align="center")

### Import package

Lets create a new file, `app.js` which we will use as our entry point. Keep in mind that the express framework is a third party package so we have to import it using the `require()` method and assigned it to a variable. The express package is a function. Lets create a variable `app` and assign the value of the express object. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707130554683/29775dea-8fdb-4816-a1a6-743e6cf670d6.png align="center")

### Create a server

To create a server using express we use the listen() method. i.e `app.listen()` .

The `listen()` method receives 2 parameters. The first is the port number while the second is a callback function. The callback function will be executed as soon as the server starts. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707130898059/7750c7ad-b747-4fa4-8f3f-6ad294444f75.png align="center")

Lets see the output on the console. run: `node app.js`👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707131045432/bb71d76e-1025-4612-b027-acec7156ec6e.png align="center")

## Handling routes and sending response

The route consists of the http method and the url.

### Handling get requests

Using the `get()` method we can handle requests on the url. The first parameter of the `get()` method is the root url. We pass a callback function as the second parameter which will be called whenever we make a <mark>get</mark> request on the url. The callback function receives 2 important parameters, <mark>request</mark> and <mark>response</mark> written as <mark>req</mark> and <mark>res</mark> . To send a response we use the `send()` method.👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707131915453/d5031f6d-e2c3-4976-b33e-4623d5bb2d91.png align="center")

Lets see the response on the web browser. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707131889196/2b6d01b9-e7e5-4a89-9f66-eb0e899a343c.png align="center")

Using <mark>method chaining</mark> we can also set the status of the response 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707132058558/c5d64a6c-be12-4b31-bf52-dea4ca7a88ff.png align="center")

Keep in mind that when we use the `send()` method, by default the content type is `text/HTML`. To send some json response we cannot use the send() method because with json response the content type is `application/json` .So to achieve this we use the `json()` method instead of the send method. 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707132614372/77bfcdba-a0b8-4977-8c31-a30e081bc5d4.png align="center")

Browser response 👇🏼

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707132634728/ea9e1b9e-5dc9-473a-880c-c75d834a72af.png align="center")

### That's all for this lesson.

**Next**: *Frontend vs Backend development.*

**See you soon**😊🫡✌🏼...
