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,
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?
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
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
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
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
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
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”
start the server and navigate to http://localhost:8080/contactus, you should get a response as below
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.
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.
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”
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….
This is why it’s best you place all global middleware before any route.