Send JSON response.
Examples:
res.json(null);
res.json({ user: 'tj' });
res.status(500).json('oh noes!');
res.status(404).json('I dont have that');
Send JSON response with JSONP callback support.
Examples:
res.jsonp(null);
res.jsonp({ user: 'tj' });
res.status(500).jsonp('oh noes!');
res.status(404).jsonp('I dont have that');
After middleware.init executed, Response will contain req property See: express/lib/middleware/init.js
Send a response.
Examples:
res.send(new Buffer('wahoo'));
res.send({ some: 'json' });
res.send('<p>some html</p>');
res.status(404).send('Sorry, cant find that');
Event emitter The defined events on documents including:
Appends the specified value to the HTTP response header field. If the header is not already set, it creates the header with the specified value. The value parameter can be a string or an array.
Note: calling res.set() after res.append() will reset the previously-set header value.
Set Content-Disposition header to attachment with optional filename
.
Clear cookie name
.
Set Content-Type response header with type
through mime.lookup()
when it does not contain "/", or set the Content-Type to type
otherwise.
Examples:
res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');
Set cookie name
to val
, with the given options
.
Options:
maxAge
max-age in milliseconds, converted to expires
signed
sign the cookiepath
defaults to "/"Examples:
// "Remember Me" for 15 minutes res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });
// save as above res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })
Transfer the file at the given path
as an attachment.
Optionally providing an alternate attachment filename
,
and optional callback fn(err)
. The callback is invoked
when the data transfer is complete, or when an error has
ocurred. Be sure to check res.headerSent
if you plan to respond.
The optional options argument passes through to the underlying res.sendFile() call, and takes the exact same parameters.
This method uses res.sendfile()
.
Respond to the Acceptable formats using an obj
of mime-type callbacks.
This method uses req.accepted
, an array of
acceptable types ordered by their quality values.
When "Accept" is not present the first callback
is invoked, otherwise the first match is used. When
no match is performed the server responds with
406 "Not Acceptable".
Content-Type is set for you, however if you choose
you may alter this within the callback using res.type()
or res.set('Content-Type', ...)
.
res.format({ 'text/plain': function(){ res.send('hey'); },
'text/html': function(){
res.send('<p>hey</p>');
},
'appliation/json': function(){
res.send({ message: 'hey' });
}
});
In addition to canonicalized MIME types you may also use extnames mapped to these types:
res.format({ text: function(){ res.send('hey'); },
html: function(){
res.send('<p>hey</p>');
},
json: function(){
res.send({ message: 'hey' });
}
});
By default Express passes an Error
with a .status
of 406 to next(err)
if a match is not made. If you provide
a .default
callback it will be invoked
instead.
Get value for header field
.
Set Link header field with the given links
.
Examples:
res.links({ next: 'http://api.example.com/users?page=2', last: 'http://api.example.com/users?page=5' });
Set the location header to url
.
The given url
can also be the name of a mapped url, for
example by default express supports "back" which redirects
to the Referrer or Referer headers or "/".
Examples:
res.location('/foo/bar').; res.location('http://example.com'); res.location('../login'); // /blog/post/1 -> /blog/login
Mounting:
When an application is mounted and res.location()
is given a path that does not lead with "/" it becomes
relative to the mount-point. For example if the application
is mounted at "/blog", the following would become "/blog/login".
res.location('login');
While the leading slash would result in a location of "/login":
res.location('/login');
Redirect to the given url
with optional response status
defaulting to 302.
The resulting url
is determined by res.location()
, so
it will play nicely with mounted apps, relative paths,
"back"
etc.
Examples:
res.redirect('/foo/bar'); res.redirect('http://example.com'); res.redirect(301, 'http://example.com'); res.redirect('http://example.com', 301); res.redirect('../login'); // /blog/post/1 -> /blog/login
Render view
with the given options
and optional callback fn
.
When a callback function is given a response will not be made
automatically, otherwise a response of 200 and text/html is given.
Options:
cache
boolean hinting to the engine it should cachefilename
filename of the view being renderedTransfer the file at the given path
.
Automatically sets the Content-Type response header field.
The callback fn(err)
is invoked when the transfer is complete
or when an error occurs. Be sure to check res.sentHeader
if you wish to attempt responding, as the header and some data
may have already been transferred.
Options:
maxAge
defaulting to 0 (can be string converted by ms
)root
root directory for relative filenamesheaders
object of headers to serve with filedotfiles
serve dotfiles, defaulting to false; can be "allow"
to send themOther options are passed along to send
.
Examples:
The following example illustrates how res.sendFile()
may
be used as an alternative for the static()
middleware for
dynamic situations. The code backing res.sendFile()
is actually
the same code, so HTTP cache support etc is identical.
app.get('/user/:uid/photos/:file', function(req, res){
var uid = req.params.uid
, file = req.params.file;
req.user.mayViewFilesFrom(uid, function(yes){
if (yes) {
res.sendFile('/uploads/' + uid + '/' + file);
} else {
res.send(403, 'Sorry! you cant see that.');
}
});
});
Set the response HTTP status code to statusCode
and send its string representation as the response body.
Set header field
to val
, or pass
an object of header fields.
Examples:
res.set('Foo', ['bar', 'baz']); res.set('Accept', 'application/json'); res.set({ Accept: 'text/plain', 'X-API-Key': 'tobi' });
Aliased as res.header()
.
Set status code
.
Set Content-Type response header with type
through mime.lookup()
when it does not contain "/", or set the Content-Type to type
otherwise.
Examples:
res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');
Adds the field to the Vary response header, if it is not there already. Examples:
res.vary('User-Agent').render('docs');
Generated using TypeDoc
Use
socket
instead.