Getting started with ExpressJS
Chapters
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
- Welcome note
- Installing NodeJS and ExpressJS
- Developing your first application with ExpressJS
- Working with Views/Templates
- Handling requests
- Application specific settings
- Midlleware and request flow
- Understanding routes
- Modularizing routes in ExpressJS
- Request Object
- Response Object
- A word on locals
- 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!
Response object actually specifies Http or HTTPS Response Object, and represents what is sent back to the client browser. You can put new cookies value and that will write to the client browser or add custom headers along with the response. The object is pretty much always referred to as res. Let's explore some of the properties and methods that comes with response object.
First one I would like to cover is the redirect method. This method can take a HTTP status code and the path you want to redirect to, or just specifying path would be enough in most cases. Here I am using status 3O1 for permanently redirect to a given path.
You can also send status separately using status method. Use status method to set the HTTP status for the response. Good thing about this method is that it is chainable alias of Node’s response.statusCode. Setting status is very handy in different scenarios.
Next I would like to mention is the set() method, you can use this method to set custom response headers. Use this method in scenarios such as when you want to send some custom information back to the client, for example, a x-powered-by header.
You can get hold of a header by using the get method, Get method returns the HTTP response header specified by field. The match is case-insensitive.
In the previous screencast, I showed you how to get cookies with request object, response object is helpful in setting the cookies. You can use response object's cookie() method to setup a cookie in browser. This method set cookie name to value. The value parameter must be a string or object converted to JSON. Cookie method also accepts third parameter, which is an options array to set up things like domain, expiry etc.
You are able to get rid of the cookie using clearCookie() method. Pass on the name of the cookie and rest will be taken care of.
Next method worth mentioning is response object's json method. This method sends JSON response. This method accepts an object or an array as parameter.
Next method is jsonp() method, this is used to Send a JSON response with JSONP support. This method is identical to res.json(), except that it opts-in to JSONP callback support.
If you want to send a file as attachment, that is if you want to force download a file to browser, you can use download() method. download() method take path to the file as first parameter, optional file name as second. When an error occurs or transfer is complete, the method calls the optional callback function
One of the most important method in response object is the render() method, This method renders a view and sends the rendered HTML string back to the client. This method takes first parameter as the view file name, second is key value pair that is made available to the view for dynamic data rendering. Third parameter is the callback function that takes error object and rendered HTML, this HTML can be fed to response object's send method.
There are many other methods that could help in development. To check them out you will have to goto the Express JS website, and under API section you will have to click on Response link to see what other methods and properties are available.
In next screencast, I will briefly touch on global variable that you want to make available to all templates.