ExpressJS: Creating Routes for ExpressJS Server

Ganesh B
4 min readOct 1, 2018

--

We have already created our first route for ExpressJS in the previous blog. We will understand creation of different routes in this article.

HTTP

HTTP is a TCP based protocol, which has 9 basic methods to work with a HTTP request. Namely: GET, PUT, POST, PATCH, DELETE, OPTIONS, CONNECT, TRACE, HEAD. You can read more about it in Mozilla documentation. We will focus on the first 6 HTTP methods as the scope of this post for the sake of simplicity and understanding for people who are new to web development.

Here is how it works:

Whenever you visit a website from a browser a lot of things happen behind the scene. The browser makes a GET request to the specified URL and gets the HTML page for you. During this it creates a connection to the server, waits for the response, and when the server sends the request it accepts the response closing the connection; finally displaying it to the browser. This is how the GET HTTP request works. But what about others? Well, it is simple. In a lot of websites, you have filled forms, right? These forms actually make a HTTP request to the server, with a request body, to create/update/delete any resource/data. These are done using HTTP POST, PUT/PATCH, DELETE methods, respectively.

All these actions happen because the server is configured to understand these requests and process them accordingly. The server’s work of understanding these requests, parsing them, and then finally responding to them is done by routes and their associated callbacks/functions, in case of ExpressJS. These callbacks or functions contain the business or processing logic to send the response back.

ROUTE AND METHOD DEFINITIONS

Take a look at the HTTP GET route we wrote before:

The above statement tells the server that if someone makes a HTTP GET request to the server at http://domain.com/ (note that the URL is / in this case) then run the function associated with it. The function, further, says that for every GET request received at the URL / sends a response with Header status 200 ok and response body as an JSON object {status: “running”, time: Date.now()} with Date.now() converted to the Date value of Javascript.

To define routes with POST, PUT, PATCH, DELETE, OPTIONS methods, just replace the app.get with respective methods for each route definition as below:

The above defines all HTTP methods GET, PUT, POST, PATCH, DELETE, OPTIONS for the URL /. Sounds simple? Yes, that is to how routes are defined.

PATH DEFINITIONS

What if you want to use a different URL other than /?

Yes, that is also possible. Look at the code below that defines a HTTP POST request on a URL /api/myform

app.post(“/api/myform”, function…)

DYNAMIC URL PARAMETERS

What if you have a dynamic parameter in the URL that you want to decide and send from the client based on user action? That can be done as well. Look at the code below:

app.post(“/api/myform/:someDynamicIdFromClient”, function…)

You can decide what the value will be in the client based on user action. Say for example, if the client wants to create a list in a category (category decided dynamically based on some selection), you can have a route defined like below:

app.post(“/api/myform/:categoryId”, function…)

You can then capture the value of categoryId in the function using the request object req like below:

https://www.domain.com/api/myform/12

Create routes and access dynamic URL Parameters as below example:

URL QUERY PARAMETERS

Like req.params.yourDynamicId is to access URL Parameters, you can access query strings from req.query.yourQueryKey. To remind, query is nothing but the key/value that you add after the ? in the URL; to send some values to the server. Take a look at the below example code:

https://www.domain.com/api/myform/12?create=later&notify=true

Create routes and access URL Query parameters as below example:

Just as an FYI, the callback/function argument that takes care of the business logic is also referred to as the Route Handler.

That is all to defining routes. To summarize, we learnt along the post:

  • How to create a simple route with method GET?
  • How we can create routes for different methods like POST, PUT, PATCH, DELETE, OPTIONS?
  • How we can create routes that listen to a different URL path?
  • How we can read request URL dynamic parameters from the Route Handler?
  • How we can read request URL Query parameters from the Route Handler?

Our final code looks like this, which we will use to extend:

You can run this code using the node command from the project folder path and see its functioning:

node index.js

But does this suffice for a basic application development? What if I want to do some processing to the request before it reaches our Route Handler? Before we delve into that we will take a deeper look into the request object req.

What I need to know about the req Request Object in the Route Request Handler: https://medium.com/@ganeshsurfs/expressjs-series-what-i-need-to-know-about-the-req-request-object-in-the-route-request-handler-b4aab9e24300

ExpressJS Series — All Blogs https://medium.com/@ganeshsurfs/expressjs-series-links-9e038be8d78b

Let me know how I did, and if you learnt something new. Do leave your comments, and dont forget to like the article.

--

--