Response Object ?

The response object (which is normally passed to a callback, meaning you can name it whatever you want: it is common to name it res, resp, or response) starts its life as an instance of http.ServerResponse, a core Node object.The most useful properties and methods of the response object (all of these are added by Express):

res.status(code)
Sets the HTTP status code. Express defaults to 200 (OK), so you will have to use this method to return a status of 404 (Not Found) or 500 (Server Error), or any other status code you wish to use. For redirects (status codes 301, 302, 303, and 307), there is a method redirect, which is preferable.

res.set(name, value)
Sets a response header. This is not something you will normally be doing manually.

res.cookie(name, value, [options]), res.clearCookie(name, [options])
Sets or clears cookies that will be stored on the client. This requires some middleware support.

res.redirect([status], url)
Redirects the browser. The default redirect code is 302 (Found). In general, you should minimize redirection unless you are permanently moving a page, in which case you should use the code 301 (Moved Permanently).

res.send(body), res.send(status, body)
Sends a response to the client, with an optional status code. Express defaults to a content type of text/html, so if you want to change it to text/plain (for example), you’ll have to call res.set(‘Content-Type’, ‘text/plain\’) before calling res.send. If body is an object or an array, the response is sent as JSON instead (with the content type being set appropriately), though if you want to send JSON, I recommend doing so explicitly by calling res.json instead.

res.json(json), res.json(status, json)
Sends JSON to the client with an optional status code.

res.jsonp(json), res.jsonp(status, json)
Sends JSONP to the client with an optional status code.

res.type(type)
A convenience method to set the Content-Type header. It is essentially equivalent to res.set(‘Content-Type’, type), except that it will also attempt to map file extensions to an Internet media type if you provide a string without a slash in it. For example, res.type(‘txt’) will result in a Content-Type of text/plain. There are areas where this functionality could be useful (for example, automatically serving disparate multimedia files), but in general, you should avoid it in favor of explicitly setting the correct Internet media type.

res.format(object)
This method allows you to send different content depending on the Accept request header. This is of primary use in APIs. Here’s a very simple example:

res.format({'text/plain': 'hi there', 'text/html': '<b>hi there</b>'}).
res.attachment([filename]), res.download(path, [filename], [callback])

Both of these methods set a response header called Content-Disposition to attachment; this will prompt the browser to download the content instead of displaying it in a browser. You may specify filename as a hint to the browser. With res.download, you can specify the file to download, whereas res.attachment just sets the header; you still have to send content to the client.

res.sendFile(path, [options], [callback])
This method will read a file specified by path and send its contents to the client. There should be little need for this method; it’s easier to use the static middleware, and put files you want available to the client in the public directory. However, if you want to have a different resource served from the same URL depending on some condition, this method could come in handy.

res.links(links)
Sets the Links response header. This is a specialized header that has little use in most applications.

res.locals, res.render(view, [locals], callback)
res.locals is an object containing default context for rendering views. res.render will render a view using the configured templating engine (the localsparameter to res.render shouldn’t be confused with res.locals: it will override the context in res.locals, but context not overridden will still be available).
Note that res.render will default to a response code of 200; use res.status to specify a different response code.

About

My Name is Dileep Singh, Noder, Javascript Lover & NoSql Developer, Fitness Freak, Love to travel new places, learning photography & Music Manic ♯ ♩ ♬

Posted in Express, Node.Js