ExpressJS
Tags: javascript, mernstack, webdev
Published: 2021-12-26 13:24:23


![Express JS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ycqi6b0n7zhurisc4c3.png)

## What is ExpressJS ?

Express js is a efficient and easy to learn web application framework. It was written in javascript as a library with different tools which helps you in creating web apps. It was developed by T.J. Holowaychuk, StrongLoop and some contributors under MIT license. This initially released in November 16, 2010. The most stable version is 4.17.2 which released in December 16, 2021. We use Node JS run-time with express to develop web apps. Express work as the back-end web server in the MERN stack application which makes the application fast and robust.

## How to install and use Express ?

We need the following tools installed prior to get express.

- [NodeJS](https://nodejs.org/en/)
- [NPM (Node Package Manager)](https://www.npmjs.com/)
- Any IDE or Code Editor. ([WebStorm](https://www.jetbrains.com/webstorm/) / [VSCode](https://code.visualstudio.com/) / [Atom](https://atom.io/) / [SublimeText](https://www.sublimetext.com/))

Download and install the LTS (Long Term Support) versions of these tools and you are ready to go.

For this tutorial I'll use VSCode as our code editor.

Create and open a folder using your editor.

We will create / initialize a nodejs project using `npm`.

Open a terminal in your editor in project's root directory. 
Enter the command below.

```
npm init --y
```
This command will initialize the project with a new file called `package.json` which includes the meta data collected from the folder that you created. To change these values we will go to this file and change it. Keywords, author & description etc.


Then we should create the index.js file which is our application's entry point or the startup of our web application's backend. After creating index.js file in the project root we have to change a value in `package.json` file. We will change the value of `main` to `index.js`. Then it should look similar as below.

```
{
  "name": "expressproject",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": ["express"],
  "author": "sndp",
  "license": "ISC",
  "description": "An express js project"
}
```

Finally we can enter the following command in the terminal and install express.


```
npm install express --save
```
This command will download and add the library to work with your project. Make sure to check package.json to be changed like below.

```
{
  "name": "expressproject",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "express"
  ],
  "author": "sndp",
  "license": "ISC",
  "description": "An express js project",
  "dependencies": {
    "express": "^4.17.2"
  }
}
```
**We have now successfully installed express in our project.**

We will create a simple server that we can use in our project and run it.

Change your `index.js` file as below.

```
const express = require('express')
const server = express();
const port = 5000;

server.listen(port, () => {
    console.log("server is listening on port " + port);
})
```

Now go to the terminal and enter the following command to run our server.

```
node index.js
```

If the terminal reads like below with no errors
`server is listening on port 5000`
Our server is running successfully.

## Why developers use expressjs ?

If you are a web developer maybe you are familiar with back-end frameworks like Spring or ASP.Net. But also you must know the express js if you develop a Node JS app. Express is the most common back-end web framework in use for a node js web application in the world today.

### Because it is easy

Working with express is easy because it is consist of all javascript based libraries. So the business code and library can work very well together.

### Cross-platform

Express can use in a wide variety of platforms. These include mobile app back-end or web application development and many web services. We can route the requests and serve every user with express js.

### Communicate with front-end

Express makes it easy to work with front-end because we don't need intermediate libraries. As an example we can use middleware functions in express to direct users before handling the requests using express. And we can use express-session and express-cookies to handle user meta data objects inside server. And there are many more add-ons we can use in our application to implement easier with the front-end.


## Uses of express js
There are many uses of building web apps and apis using express js.


### To develop web apps

To handle client's requests and handle them by giving responses and directing to the pages or provide implemented templates directly to the user with express. Also it is easy serving and working with static files like stylesheets (css), scripts (js) and images.

### Developing ReSTful Web Services (APIs)

Since the progress of web development technologies, apps based on microservices architecture were becoming relevant. From monolithic to microservices architecture they are evolving. A web application basically started after developing an API. Making a API using express js is very easy. It is just a couple of routing methods and connection to a data source away.

### Developing other javascript libraries.

Many javascript libraries and frameworks uses express or had used express js in their implementation of libraries. There are many interesting work done with using express framework.
These include, 
- [Feathers](http://feathersjs.com/) - Build prototypes in minutes, 
- [ItemsAPI](https://www.itemsapi.com/) - Search backend for web and mobile apps built on  Express and Elastisearch.
- [KeystoneJS](http://keystonejs.com/) - Website and API Application Framework etc.


Many companies use express js eg. IBM and UBER in their applications and it is one of the major step of building a node js based web apps.

To learn more on expressjs refer the link below.

https://expressjs.com/en/starter/installing.html