Response
Server HTTP response. Often referred to as res
parameter.
Constructor
This section is empty.
Properties
serverResponse
Type: http.ServerResponse
Underlying
http.ServerResponse
object. Can be used to access lower-level API of the http
Node.js module.
Properties
This section is empty.
Methods
earlyHints
Signature:
earlyHints(hints: Record<string, string | string[]>): Promise<void>
where
hints
:Record<string, string | string[]>
— Early Hints response headers to be sent
Sends a 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')})
end
Signature:
end(): Promise<void>
Sends all response headers and body
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
json
Signature:
json(body: object): Promise<void>
where
body
:object
— response body
Sends response headers and body of application/json
content type.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
text
Signature:
text(body: string): Promise<void>
where
body
:string
— response body
Sends response body of text/plain
content type.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
redirect
Signature:
redirect(location: string): Promise<void>
where
location
:string
— path of the redirect
Redirects request to a specified location.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
render
Signature:
render(source: string, options?: Record<string, unknown>): Promise<void>
where
source
:string
— source content of the templateoptions
:Record<string, unknown> | undefined
— template options
Renders an HTML template with the
RenderFunction
, which must be specified with
the LunaticServer.renderer()
method.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
renderFile
Signature:
renderFile(path: string, options?: Record<string, unknown>): Promise<void>
where
path
:string
— path to the template fileoptions
:Record<string, unknown> | undefined
— template options
Renders an HTML template from a file with the
RenderFunction
, which must be specified with
the LunaticServer.renderer()
method.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
send
Signature:
send(content?: string | Buffer, mimetype?: string): Promise<void>
where
content
:string | Buffer | undefined
— response bodymimetype
:string | undefined
— MIME-type of the response body
Sends response body. If content
is empty or undefined
, response status is
set to 204 No Content
. mimetype
defaults to application/octet-stream
.
After this method is called, the response cannot send any more headers or body, and is considered complete. 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.
sendFile
Signature:
sendFile(path: string): Promise<void>
where
path
:string
— path to the file
Sends specified file as response body. MIME-type is inferred from the extension
and defaults to application/octet-stream
.
setCookie
Signature:
setCookie(name: string, value: number | string, options?: CookieOptions): Response
where
name
:string
— name of the cookievalue
:number | string
— value of the cookieoptions
:CookieOptions | undefined
— attributes of theSet-Cookie
HTTP header
Sets a cookie for this request. The second argument specifies attributes of the
Set-Cookie
header.
clearCookie
Signature:
clearCookie(name: string, options?: CookieOptions): Response
where
name
:string
— name of the cookieoptions
:CookieOptions | undefined
— attributes of theSet-Cookie
HTTP header
Clears cookie. Some cookie attributes, specified with second argument options
,
such as Path
and Domain
, should be same as when cookie was set.
setHeader
Signature:
setHeader(name: string, value: number | string | string[]): Response
where
name
:string
— name of the headervalue
: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.
setHeaders
Signature:
setHeaders(headers: Record<string, number | string | string[]>): Response
where
headers
:Record<string, number | string | string[]>
— headers to be set
Sets multiple response headers by calling Response.setHeader()
for every specified header.