1.0.0-alpha.3 • Published 6 years ago
aldo-http v1.0.0-alpha.3
Aldo-http
is an enhanced HTTP createServer
module for Node.js.
const { createServer } = require('aldo-http')
// server
const server = createServer(() => "Hello world!")
// start
server.start(3000)
createServer
declare function createServer (options: Options, fn: RequestHandler): Server;
declare function createServer (fn: RequestHandler): Server;
declare function createServer (options: Options): Server;
declare function createServer (): Server;
declare interface Options {
tls?: https.ServerOptions;
}
HTTPS server
const { readFileSync } = require('fs')
const { createServer } = require('aldo-http')
const options = {
tls: {
key: readFileSync('path/to/key/file.pem'),
cert: readFileSync('path/to/cert/file.pem')
// see `https.createServer()` for more options
}
}
// make a HTTPS server using the TLS options
const server = createServer(options, () => 'Hello world!')
server.start({
port: 443,
exclusive: true,
host: 'example.com'
})
Request handler
The request
event handler could be a common or an async function.
Each handler will receive the http.IncomingMessage
object as a request, and could return anything as a response.
declare type RequestHandler = (request: http.IncomingMessage) => any;
The handler's output could be anything:
streams
will be pipedstrings
andbuffers
will be sent as isnulls
andundefined
values will be considered as empty responses (Status code 204)- anything else will be serialized as
JSON
.
To get more control over the response to send, Response instances could be used.
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): void; // send the response to the client (used internally)
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 the constructor
or 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.
const { createServer, createTextResponse } = require('aldo-http')
// handler
const handler = () => createTextResponse("Hello world!")
// server
const server = createServer(handler)
// start
server.start(3000)
1.0.0-alpha.3
6 years ago
1.0.0-alpha.2
6 years ago
1.0.0-alpha
6 years ago
0.2.2
6 years ago
0.2.1
6 years ago
0.2.0
6 years ago
0.1.3
6 years ago
0.1.2
6 years ago
0.1.1
6 years ago