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!

There can be instances when you want to make certain data available to all the views or templates. This data can be an object or any string that you would like to get hold of inside the view files.

Let's start by creating a couple of routes, first one that I am going to create is for the home page. I will use the callback function to render index.jade view.

I will copy this and paste it to create a new route. Let's call this the download route. I will utilize my download.jade view here.

Now I will use app.locals object to add properties to, this object make its properties available as local variables within the application including the view files. So let's add a property called title and give it a value of "My Website".

Now I will go back to my index.jade file and will just print the value of title variable within h2 tag. For the second route we have in our application, I have to create a file download.jade under my views folder for the render method to find it. I will open the download.jade file and output title value inside H3 tag.

I can launch my application using nodemon now. Once ready, I can open my browser to browse to my home page. As you can see that it nicely print title variable value. Similarly If I change the URL to add download at the end, you will notice that title get printed in a smaller font because we enclosed it within the H3 tags.

There are many scenarios that you would want to use application locals, but this will depend on case to case.