Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface Response<ResBody, Locals, StatusCode>

Type parameters

  • ResBody = any

  • Locals: Record<string, any> = Record<string, any>

  • StatusCode: number = number

Hierarchy

Index

Properties

app: Application<Record<string, any>>
charset: string
headersSent: boolean
json: Send<ResBody, Response<ResBody, Locals, StatusCode>>

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');
jsonp: Send<ResBody, Response<ResBody, Locals, StatusCode>>

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');
locals: Locals
req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>

After middleware.init executed, Response will contain req property See: express/lib/middleware/init.js

send: Send<ResBody, Response<ResBody, Locals, StatusCode>>

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');

Methods

  • append(field: string, value?: string | string[]): Response<ResBody, Locals, StatusCode>
  • 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.

    since

    4.11.0

    Parameters

    • field: string
    • Optional value: string | string[]

    Returns Response<ResBody, Locals, StatusCode>

  • attachment(filename?: string): Response<ResBody, Locals, StatusCode>
  • Set Content-Disposition header to attachment with optional filename.

    Parameters

    • Optional filename: string

    Returns Response<ResBody, Locals, StatusCode>

  • Clear cookie name.

    Parameters

    Returns Response<ResBody, Locals, StatusCode>

  • contentType(type: string): Response<ResBody, Locals, StatusCode>
  • 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');

    Parameters

    • type: string

    Returns Response<ResBody, Locals, StatusCode>

  • cookie(name: string, val: string, options: CookieOptions): Response<ResBody, Locals, StatusCode>
  • cookie(name: string, val: any, options: CookieOptions): Response<ResBody, Locals, StatusCode>
  • cookie(name: string, val: any): Response<ResBody, Locals, StatusCode>
  • Set cookie name to val, with the given options.

    Options:

    • maxAge max-age in milliseconds, converted to expires
    • signed sign the cookie
    • path 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 })

    Parameters

    Returns Response<ResBody, Locals, StatusCode>

  • Parameters

    Returns Response<ResBody, Locals, StatusCode>

  • Parameters

    • name: string
    • val: any

    Returns Response<ResBody, Locals, StatusCode>

  • download(path: string, fn?: Errback): void
  • download(path: string, filename: string, fn?: Errback): void
  • download(path: string, filename: string, options: any, fn?: Errback): void
  • 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.headersSent 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().

    Parameters

    Returns void

  • Parameters

    • path: string
    • filename: string
    • Optional fn: Errback

    Returns void

  • Parameters

    • path: string
    • filename: string
    • options: any
    • Optional fn: Errback

    Returns void

  • format(obj: any): Response<ResBody, Locals, StatusCode>
  • 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.

    Parameters

    • obj: any

    Returns Response<ResBody, Locals, StatusCode>

  • get(field: string): undefined | string
  • Get value for header field.

    Parameters

    • field: string

    Returns undefined | string

  • header(field: any): Response<ResBody, Locals, StatusCode>
  • header(field: string, value?: string | string[]): Response<ResBody, Locals, StatusCode>
  • Parameters

    • field: any

    Returns Response<ResBody, Locals, StatusCode>

  • Parameters

    • field: string
    • Optional value: string | string[]

    Returns Response<ResBody, Locals, StatusCode>

  • links(links: any): Response<ResBody, Locals, StatusCode>
  • location(url: string): Response<ResBody, Locals, StatusCode>
  • 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');
    

    Parameters

    • url: string

    Returns Response<ResBody, Locals, StatusCode>

  • redirect(url: string): void
  • redirect(status: number, url: string): void
  • redirect(url: string, status: number): void
  • 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('back'); 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

    Parameters

    • url: string

    Returns void

  • Parameters

    • status: number
    • url: string

    Returns void

  • deprecated

    use res.redirect(status, url) instead

    Parameters

    • url: string
    • status: number

    Returns void

  • render(view: string, options?: object, callback?: (err: Error, html: string) => void): void
  • render(view: string, callback?: (err: Error, html: string) => void): void
  • 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 cache
    • filename filename of the view being rendered

    Parameters

    • view: string
    • Optional options: object
    • Optional callback: (err: Error, html: string) => void
        • (err: Error, html: string): void
        • Parameters

          • err: Error
          • html: string

          Returns void

    Returns void

  • Parameters

    • view: string
    • Optional callback: (err: Error, html: string) => void
        • (err: Error, html: string): void
        • Parameters

          • err: Error
          • html: string

          Returns void

    Returns void

  • sendFile(path: string, fn?: Errback): void
  • sendFile(path: string, options: any, fn?: Errback): void
  • Transfer 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.headersSent 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 filenames
    • headers object of headers to serve with file
    • dotfiles serve dotfiles, defaulting to false; can be "allow" to send them

    Other 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.');
    }
    });
    });
    api

    public

    Parameters

    Returns void

  • Parameters

    • path: string
    • options: any
    • Optional fn: Errback

    Returns void

  • sendStatus(code: StatusCode): Response<ResBody, Locals, StatusCode>
  • Set the response HTTP status code to statusCode and send its string representation as the response body.

    link

    http://expressjs.com/4x/api.html#res.sendStatus

    Examples:

    res.sendStatus(200); // equivalent to res.status(200).send('OK') res.sendStatus(403); // equivalent to res.status(403).send('Forbidden') res.sendStatus(404); // equivalent to res.status(404).send('Not Found') res.sendStatus(500); // equivalent to res.status(500).send('Internal Server Error')

    Parameters

    • code: StatusCode

    Returns Response<ResBody, Locals, StatusCode>

  • sendfile(path: string): void
  • sendfile(path: string, options: any): void
  • sendfile(path: string, fn: Errback): void
  • sendfile(path: string, options: any, fn: Errback): void
  • deprecated

    Use sendFile instead.

    Parameters

    • path: string

    Returns void

  • deprecated

    Use sendFile instead.

    Parameters

    • path: string
    • options: any

    Returns void

  • deprecated

    Use sendFile instead.

    Parameters

    Returns void

  • deprecated

    Use sendFile instead.

    Parameters

    • path: string
    • options: any
    • fn: Errback

    Returns void

  • set(field: any): Response<ResBody, Locals, StatusCode>
  • set(field: string, value?: string | string[]): Response<ResBody, Locals, StatusCode>
  • 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().

    Parameters

    • field: any

    Returns Response<ResBody, Locals, StatusCode>

  • Parameters

    • field: string
    • Optional value: string | string[]

    Returns Response<ResBody, Locals, StatusCode>

  • status(code: StatusCode): Response<ResBody, Locals, StatusCode>
  • Set status code.

    Parameters

    • code: StatusCode

    Returns Response<ResBody, Locals, StatusCode>

  • type(type: string): Response<ResBody, Locals, StatusCode>
  • 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');

    Parameters

    • type: string

    Returns Response<ResBody, Locals, StatusCode>

  • vary(field: string): Response<ResBody, Locals, StatusCode>
  • Adds the field to the Vary response header, if it is not there already. Examples:

    res.vary('User-Agent').render('docs');
    

    Parameters

    • field: string

    Returns Response<ResBody, Locals, StatusCode>