Watching:  

Getting started with ExpressJS


Author: Subject Coach
Added on: 5th Mar 2015

 
Please note: You need to login to view this resource

This course is for beginners who wants to get started with ExpressJS framework for NodeJS. This course is subdivided into 13 parts as listed below

  1. Welcome note
  2. Installing NodeJS and ExpressJS
  3. Developing your first application with ExpressJS
  4. Working with Views/Templates
  5. Handling requests
  6. Application specific settings
  7. Midlleware and request flow
  8. Understanding routes
  9. Modularizing routes in ExpressJS
  10. Request Object
  11. Response Object
  12. A word on locals
  13. Conclusion

Please let us know how we can do better next time by leaving your feedback. 

 

Author: Subject Coach
Added on: 5th Mar 2015

Please get in touch with your teacher or tutor in case you have a question related to this lesson

None just yet!

A router you can say, is a small application that runs within Express application. A Router instance is a complete middleware and routing system. There are similar middle wares available to Router as are with Express application. A router has access to its own use method, verb methods, param method or route method etc.

There are many instances where multiple verb methods are used for the same route, if you have to change the route path then you have to do that for all verb methods. Route method simplifies that for us, by chaining the verb methods for a single route. We then have to change path of route at one place rather than in multiple places. This is much cleaner approach.

We can also make use of router and rather than using app instance we can create multiple route objects to handle different paths. Say for instance, in our current example, I will create a router object and rather than using app.use, I can use router.use instead. I will get rid of catch all route, that we learnt about in last video. You have to remember, this will not work as such, we have to provide our router to our application to use, in the use method, I am providing a route path and associated router.

Let's save this file and run it using nodemon. Once its ready we can launch our browser. We can see that our application is handling request in a similar fashion, as it did without the router instance.

Once your application gets bigger and keep growing, it will be important to separate some of your routing code into its own file and it's pretty easy to accomplish that. This what we can refer to as route modularization. So first, I'm going to create a couple of routes as version1 and version2. These 2 variables hold a separate instance of Router object. Now for each router objects, I'll add get handlers for path /download. I will copy and paste the code I did for the first get handler, and create an equivalent for second router object.

We need to move this code to individual files, I'll create a file with name version1.JS and cut paste the code we did for two router objects from application file to version1.JS file. I'll now remove all the instances of version 2 object from this file. Also because this file will represent a Node module, I have to change the reference of express. to require express. I'll also have to export this router object has module.

I'll now create a second file with name version2.JS and do exact same thing I did before. I'll paste the code I have in my current clipboard to version2.JS file. I'll remove all the instances of version 1 object from this file. Also because this file will represent a Node module too, I have to change the reference of express. to require express. I'll also have to export this router object has module.

Once done, I will then go edit my application file and add 2 require statements to make my modules available to my app. All I am doing here is to provide path to the file names without extensions and node will do the rest. I'll now create use these modules in my app using use method. Version1 will be used with path /version1 and version2 be used for router path /version2.

When I launch my browser and try path version1/download, you see that it actually doing nothing. I may have made a mistake. Ok I need to use response send method to send a response back to the browser rather than using console.log. After this change in both the route module files, when I try the previous request again, message is printed on screen for the first route and let's change the URL to say version 2, our second route gets a hit.

It is very handy feature of Express to allow modularizing the routes. After all the manageability and maintainability of your code increase tremendously.

In next video, I'll code the request Object.