@aldojs/response
v1.0.0
Published
HTTP Response utilities
Downloads
2
Readme
HTTP(S) response utilities to use in conjunction to @aldojs/http module.
Install
$ npm add @aldojs/response
Example
let { createServer } = require('@aldojs/http')
let { createTextResponse } = require('@aldojs/response')
// handler
let handler = () => createTextResponse("Hello world!")
// server
let server = createServer(handler)
// start
await server.start(3000)
The response whould be
HTTP-1.1 200 OK
Content-Type: plain/text
Content-Length: 12
Date: ...
Hello world!
Response
The response instance let you construct a complex response with status code, body and headers.
declare class Response {
body: any;
statusCode: number;
statusMessage: string;
headers: http.OutgoingHttpHeaders;
constructor(body?: any);
type(value: string): this; // set the `Content-Type` header
etag(value: string): this; // set the `ETag` header
length(value: number): this; // set the `Content-Length` header
location(url: string): this; // set the `Location` header
has(header: string): boolean; // check the given header is already set
remove(header: string): this; // remove the give header
setCookie(value: string): this; // append a `Set-Cookie` header
vary(...headers: string[]): this; // append a `Vary` header
send(res: http.ServerResponse): any; // send the response to the client
lastModified(value: string | Date): this; // set the `Last-Modfied` header
status(code: number, message?: string): this; // set the status code and message
append(header: string, value: string | string[]): this; // append a header value
get(header: string): string | number | string[] | undefined; // get the header value
set(header: string, value: string | number | string[]): this; // set the header value
set(headers: { [field: string]: string | number | string[]; }): this; // set multiple headers
reset(headers?: { [field: string]: string | number | string[]; }): this; // reset the headers
}
To create Response instances, you may use one of the available factories:
createRespnse(content?)
to create a response based on the given content.createEmptyResponse()
to create an empty response (default status code204
).createHtmlResponse(html)
to create a HTML response, sets theContent-Type
header totext/html; charset=utf-8
and theContent-Length
header.createTextResponse(text)
to create a text response, sets theContent-Type
header totext/plain; charset=utf-8
and theContent-Length
header.createBufferResponse(buff)
to create a buffered response, sets theContent-Type
header toapplication/octet-stream
and theContent-Length
header.createStreamResponse(stream)
to create a streamed response, sets theContent-Type
header toapplication/octet-stream
.createJsonResponse(object)
to create a JSON response, sets theContent-Type
header toapplication/json; charset=utf-8
and theContent-Length
header.