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!

The request object represents the HTTP request. It has properties for the request query string, parameters, body, HTTP headers etc. I will not cover all of the things a request object represents. However let's check some functions and properties that you are very likely to use. Of these, the first one I would like to mention is params objects.

With params object, you are able to get hold of properties mapped to the named route “parameters”. So in this path articleID represents a parameters and params object will have a property mapped automatically which can be accessed from params object. So I can replace the attribute with articleID to get hold of its value. If route path is blog/123 then 123 will the value of articleID.

Next thing I would like to mention is the query object, An object containing a property for each query string parameter in the route. If there is no query string, it is the empty object, {}. So if our URL contain a query string such as val=something, then you can get hold of val as a property val under query object, that Express creates automatically.

Next method I would like to mention is the accepts method. This method can be used to check if the specified content types are acceptable, based on the request’s Accept HTTP header field. The method will returns the best match, or it returns undefined if none of the specified content types is acceptable.
The type value may be a single MIME type string, or it can an extension name such as “JSON”, a comma-delimited list, or an array. This method returns the best match for a list or an array. You can check on Mimes such as text/plain or text/HTML etc.

Next object to mention under request object is, the cookies object. Really handy when you are using browser cookies. Created cookies for a domain are mapped to local attributes or properties to the cookies object. So say if you have set a cookie with name, Name. Then you are able to find Name property, under cookies object.

Next object to mention is the route object, This request property holds the currently-matched route, a string. In our case its /blog/:articleID. But if you want to get actual URL then you would use originalUrl property of request object. This property is much like req.url, however, it retains the original request URL, allowing you to rewrite req.url freely for internal routing purposes. In our case if articleID was 123 then originalUrl will hold value /blog/123.


While I mentioned how to get attributes from params and query objects, we have covered in previous videos on, how to get parsed body params. Request body holds key-value pairs of data submitted in the request body.

If you are unsure which objects hold the value you are interested in, then you can use param method and pass on the the param name you are interested to get value of. Param method will first check if param is present in params object, then it will check if its present in body object, and then tries the query object.


You can also get the remote user IP connecting to your application using request object's IP property.

For more on request object, you can always goto the express API page and under section Request you will see all the supported properties and methods, such as hostname property returns the hostname from the host HTTP header and secure tells you if connection is over HTTPS.


In next video, I will cover the response object.