What is express.js or express ?

If you are searching for this topic, then you have an idea about node.js or at least javascript. Express is a web application framework for node.js. In another words, it is basically a software framework that allow us to build and run web applications on top of nodeJS. Express to nodeJS is like Sinatra to ruby or ruby. Now you may have a basic idea about ExpressJS.
Node is not initially developed for web application development. But with the help of ExpressJS, we can now easily buld and run web applications.
According to the stats, express is the 4th most used package that used in npm.

Alright, now you know little bit about ExpressJS. But not strong enough to install it to your laptop.
Let’s see why ExpressJS is good?
- Ability to route
- Ability to write middlewear
- Supports MVC architecture
- Buld restfull APIs fast and easily
- Supports templating engine (Jade and EJS)
Now let’s see how we can install ExpressJS.
npm install express
Above code will install express package to your application. Now that you have express installed, you have to require the package like below line 1.
After you require the express, you have to create an express application by calling express() method.
Alright, now we have main ingredient that we need to build our expressJS simple app. The main advantage that we take from expressJS is ability to route. In routing, we define the responds to available endpoints. As an example lets think that we want to display a plain text when you go to ‘/greeting’ endpoint. Basic structure of a route is like below.
Okay, by using above dummy, lets build a simple route to ‘greetings’ endpoint.
Line 8 define the port number that we need to load when we run out file. You can simply run above node code and go to the localhost:3000/greetings url and your browser will display Greetings hooman! message.

Now we have a working 10 lines code in our hands. But we still have no idea about app.get() method. In here, getmean the HTTP method that we apply in our code. There are several HTTP methods available(Post, Get, Put, Delete, Patch). FYI express supports all these methods. In out get() routing method, we have one argument and a callback function. As the argument, we give the path or the endpoint that we need to direct. As the callback function, we need to provide a function that execute after our app direct to the given path. In our case, we provide a path ‘/greetings’ and once we direct to that path, we have a simple text display in our browser saying “Greetings hooman!”.
Another thing to keep in mind, above express app route is defined by app.get which is a way of creating routes at the top level of your application. Alter to this method, we can use router.get method. If you are creating route using this method, you have to define Router() at the top of the code like below.
const router = express.Router()
And just like that we simply build a small app using express.
