Skip to main content

Response

Server response that is passed as the second argument to the middleware (res).

Methods

res.clearCookie(name: string, options?: CookieOptions): Response

  • name: string - name of the cookie
  • options: CookieOptions - Cookie header attributes

Clears cookie. Some cookie attributes, specified with second argument options, such as Path and Domain, should be same as when cookie was set.

res.earlyHints(hints: Record<string, string | string[]>): Promise<void>

  • hints: Record<string, string | string[]> - response headers to be sent

Sends 103 Early Hints information response:

app.get('/', async (_req, res) => {
await res.earlyHints({
link: '<style.css/>; rel=preload; as=style',
})

await res.status(200).renderFile('templates/index.html')
})

res.end(): Promise<void>

Sends all response headers and body.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.json(body: object): Promise<void>

  • body: object - response body

Sends response headers and body of application/json content type.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.redirect(location: string): Promise<void>

  • location: string - path to which the request is redirected

Redirects request to another URL by setting Location header.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.removeHeader(name: string): Response

  • name: string - name of the header to be removed

Removes response header.

res.removeHeaders(headers: string[]): Response

  • headers: string[] - headers to be removed

Removes multiple headers by calling res.removeHeader() for every specified header.

res.render(source: string, options?: Record<string, unknown>): Promise<void>

  • source: string - the source content of the template
  • options: Record<string, unknown> - template options

Renders an HTML template with RenderFunction, which is specified by LunaticServer.renderer() method.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

See Template engines.

res.renderFile(path: string, options?: Record<string, unknown>): Promise<void>

  • path: string - path to the template
  • options: Record<string, unknown> - template options

Renders an HTML template file RenderFunction, which is specified by LunaticServer.renderer() method.

See Template engines.

res.send(content?: string | Buffer, mimetype?: string): Promise<void>

  • content: string | Buffer - response body
  • mimetype: string - MIME-type of the body

Sends body content of specified content type mimetype. If content is undefined, sends status 204 No Content. If mimetype is not specified, server will set default Content-Type value application/octet-stream.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.sendFile(path: string): Promise<void>

  • path: string - path to the file

Sends file as response body. Content-Type header is determined by file extension, if extension is unknown, server will set default Content-Type value application/octet-stream.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.setCookie(name: string, value: number | string, options?: CookieOptions): Response

  • name: string - name of the cookie
  • value: number | string - value of the cookie
  • options: CookieOptions - Cookie header attributes

Sets a cookie by setting Set-Cookie response header. Second argument options of type CookieOptions specifies Set-Cookie header attributes.

res.setHeader(name: string, value: number | string | string[]): Response

  • name: string - name of the header
  • value: number | string | string[] - value of the header

Sets response header. If the specified value is an array of strings, the header will be set for every element in the array.

res.setHeaders(headers: Record<string, number | string | string[]>): Response

  • headers: Record<string, number | string | string[]> - headers to be set

Sets multiple response headers by calling res.setHeader() for every specified header.

res.status(status: number): Response

  • status: number - status code of the response

Sets response status.

res.text(body: string): Promise<void>

  • body: string - response body

Sends response body of text/plain content type.

After this method, the response cannot send any other header or body, and considered completed. As with any other method that sends a response, calling another such method after the response has already been sent will result in an error.

res.withoutBody(): Response

This method signals response to not send response body and respond with status 204 No Content. Can be used to handle HEAD requests: it allows to set same headers as if the method were not called, but omit the response body.

Properties

res.serverResponse

Type: http.ServerResponse

Underlying http.ServerResponse object. Can be used to access lower-level API of http Node.js module.