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!

In this video, I will show you some application settings, that you may use in your express application. For simple and very basic web applications, some settings that I will cover are seldom used, however many developers prefer some control on different aspects of development technology they use, Express is cool enough to allow that flexibility and control to developers.

First thing we will look at is a, Set method!, Set method takes first parameter as the setting, and second parameter as value. Generally the setting is picked from the the default application settings provided by express as shown under application settings section. For boolean values, calling Set method is equivalent to calling, enable or disable methods.

You can set Environment mode using Set method with setting, ENV. Default value for this setting is undefined. You can specify if you are in development mode. You can also directly assign this value to process.env.NODE_ENV property.

Here I am setting ENV setting, to get this property, I will use Get function and output value of ENV setting to my console. Get method here is not the same Get as is used to create routes. Express is smart enough to make out the difference between the use of routing Get and Setting Get method. Here on the API page you can read more on two different Get methods. One takes single parameter and second one is overloaded to accept range of parameters.

Another method I can show you is the Enable method. This method is used to enable certain things. Here I am enabling trust proxy, this will not be such a requirement for small websites. If I want to check if certain setting is enabled or not, I will use Enabled method to do that.

Like Enable I can use Disable method to disable a setting. One the next line I will disable trust proxy!. To check if the setting has been disabled, I will use Disabled method and output the return to my console. When I save my application and start it with NoDemon, I can see that my console prints the information that I was expecting. It printed dev for ENV setting, and correctly print true, for trust proxy enabling and disabling.


Next thing I can show you is the JSONP callback function labeling, We have been using callback parameter in query string to indicate the callback name. You can override the default by using "JSONP callback name" setting. Here I cam using CB as my callback name, CB can output some kind of data for the client. Changing it is generally not recommended for public APIs but is Ok if APIs are just for your own consumption.

Next thing I can show you is the JSON replacer setting, At times we have to filter some data before it is returned to the client. JSON replacer does fiddle with javascript object before its returned. In one example say you have some data that you want to return, but it has some properties that you don't want to send, for instance within a data object you don't want to send the password value, I can do that in my JSON replacer callback function, check if the attribute name is PWD, then return undefined as value else just return the actual value. res.json internally use JSON.stringify method and JSON replacer manipulates on object before being stringified. You cannot use the replacer function to remove values from an array. If you return undefined or a function then null is used instead.

Next thing I can show you is, to use "case sensitive routing", generally by default, route paths with mixed cases are treated same, by if you enable this setting, then uppercase F in route Foobar, is treated differently to lowercase f in foobar. This setting is disabled by default.

Next thing I can show you is, to use setting called "strict routing". This setting is disabled by default. When you enable strict routing, paths "/foo" and "/foo/" are recognized differently by the router.

You can also tell Express to cache views for sometime by enabling "view cache" setting. This way rendered views are saved for some time and does not need to be regenerated for all requests.

Express is good at picking the template engine to use by file extensions. However, you can also explicitly tell Express which templating engine to use by setting "view engine" setting. I have set it up to Jade. When I do that, I am no longer required to add the view file extension. I can change the templating engine to whatever I want to use.


By default, Express tries to find views under views folder, I can set the views setting to tell express which folder or folders it can find my templates under. Here I changed the views path to TPL folder.

By default, Express add a header called X-powered-by in response, you can leave it like that or you can disable it as well using "disable" method.

We covered a fair bit of things in this chapter, you should just have a good read and try to get familiar with various settings to master Express.

In next chapter, We will learn to use middleware in Express and also will check how request flow works.