Routes and Middleware in Node.js/Express, Simplified

Benson
Benson
Nov 12 · 5 min read
Image for post
Image for post

What comes into your mind when you hear of routing in an application?

This refers to how an application responds to a request from the client side to a particular endpoint with a specific http request method (POST,PUT,DELETE, etc.), you get it? if not, Don’t worry…

In simple terms, routing refers to how the application behaves on a specific url or path with a specific HTTP method

For example, let’s say you have a 3-page website with URL (http://mywebsite.com), which includes the Home page, contact us page and an about us page,

Image for post
Image for post
Just a simple header to display 3 links

so this is where routing comes into play, with a get request to the homepage i.e. (http://mywebsite.com), you intend to respond with the home or landing page where u have these fancy carousel and all making it look great, isn’t it?

Image for post
Image for post
Credit to @uiraygan on IG for the awesome UI design

but for a get request to the URL (https://ourwebsite.com/contactus), obviously we intend to respond with some sort of form where a user could input details such as email, name and leave a message for us

Image for post
Image for post

So with this basic explanation of routing in an application, I hope you get what exactly i am trying to put across…yep!

Now Lets See How We Can Create Simple Routes Using Different HTTP Methods With Code Samples (Node.js/express)

Before we continue make sure you have node js and postman installed, you can click on the links to download from their official website, we would use post man for testing our routes with a HTTP POST method

Say we have a VERY SIMPLE express application up and running on port 8080

Image for post
Image for post

NB: I am able to use Es6 syntax by just adding “type”: “module” to my package.json file.

Now, the syntax for defining routes in an express application includes

app.method(path, callback)

where method refers to the HTTP method/verb (get,post,put etc.) to apply, path refers to the route or URL at which the request will apply, also callback is the function or middleware(explained down below) which will run when a request an http method hit to path.

Consider making a get request to the route “/” as shown below

Image for post
Image for post
get request to the route (‘’/”)

NB: Notice the callback function takes in two params, i.e. request and response, it also takes in an optional third params (function) which is called when there is another middleware to run

Don’t worry, we will look into middleware soon

start the server and navigate to http://localhost:8080/

Hurray!!! You just made your first route, and you should get a response below as this

Image for post
Image for post

You should see the same response as above, i.e. which means it has successfully made a get request to “/”, also, lets try making a get request to the contact page as “http://localhost:8080/contactus”

Image for post
Image for post

start the server and navigate to http://localhost:8080/contactus, you should get a response as below

Image for post
Image for post

With this simple explanation of routing, Hope you love and leave some love….

What is a middleware?

Middleware functions are functions which have access to both request and response object and the next middleware function in the application’s request-response cycle…..You still don’t get it? Don’t worry….

In simple terms, as the name suggests, “middle”, this is a function or a program which runs between the time a server gets a request and the time the server sends the response out to the client.

There are two main types of middleware, i.e. Global middleware(This is a middleware which can be accessed by all routes in the application hence its name “Global”) and specific middleware (This is a middleware which applies to just a specific route)

Using a global middleware in an express application makes use of the syntax app.use(middleware);

NB: one thing to take note of is middleware runs in order in order in which they are defined (one after the other), Don’t worry we would see code samples soon.

Image for post
Image for post

From The above code sample, checkIsAuth is a global middleware(applies to all routes) which checks if a user is authentication before accessing a route,

when I start my server and navigate to (http://localhost:8080/secret)…i won’t get access to the that page but rather log to the console “Not logged in” because the “middleware” checks to see if i am authenticated or not.

Image for post
Image for post

but when isAuth flag is set to true, i can now have access to all other routes.

Now let’s me show you a good example of “middleware runs in order of definition”

Image for post
Image for post

Notice where the middleware is applied (app.use(checkIsAuth))? it comes after all other routes, now even though the isAuth flag is false, I still do get access to all secret routes before the middleware is applied!!!.

for example when I navigate to (http://localhost:8080/secret) I am able to access that page….

Image for post
Image for post

This is why it’s best you place all global middleware before any route.

JavaScript In Plain English

New JavaScript + Web Development articles every day.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store