# aldo-http

> Enhanced HTTP `createServer` module for Node.js

Latest version **1.0.0-alpha.3** (published 2018-06-04) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install aldo-http
pnpm add aldo-http
yarn add aldo-http
bun add aldo-http
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.0-alpha.3 |
| Published | 2018-06-04 |
| First published | 2018-02-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=8 |
| Dependencies | 2 |
| Unpacked size | 32.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | absolux |
| Maintainers | absolux |
| Keywords | aldo, http, server, request, response, create-server |

## Links

- npm: https://www.npmjs.com/package/aldo-http
- Repository: https://github.com/aldojs/http
- Homepage: https://github.com/aldojs/http#readme
- Issues: https://github.com/aldojs/http/issues
- npm.io page: https://npm.io/package/aldo-http

## Dependencies (2)

- [mime-types](https://npm.io/package/mime-types.md) ^2.1.18
- [@sindresorhus/is](https://npm.io/package/@sindresorhus/is.md) ^0.9.0

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.0.0-alpha.3 (latest) — 2018-06-04
- 1.0.0-alpha.2 — 2018-05-20
- 1.0.0-alpha — 2018-05-18
- 0.2.2 — 2018-05-08
- 0.2.1 — 2018-03-20
- 0.2.0 — 2018-03-11
- 0.1.3 — 2018-02-18
- 0.1.2 — 2018-02-10
- 0.1.1 — 2018-02-07

## README

`Aldo-http` is an enhanced HTTP `createServer` module for Node.js.

```js
const { createServer } = require('aldo-http')

// server
const server = createServer(() => "Hello world!")

// start
server.start(3000)
```

## createServer

```ts
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

```js
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.

```ts
declare type RequestHandler = (request: http.IncomingMessage) => any;
```

The handler's output could be anything:
- `streams` will be piped
- `strings` and `buffers` will be sent as is
- `nulls` and `undefined` 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](#response) instances could be used.

## Response

The response instance let you construct a complex response with status code, body and headers.

```ts
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](#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 code `204`).
- `createHtmlResponse(html)` to create a HTML response, sets the `Content-Type` header to `text/html; charset=utf-8` and the `Content-Length` header.
- `createTextResponse(text)` to create a text response, sets the `Content-Type` header to `text/plain; charset=utf-8` and the `Content-Length` header.
- `createBufferResponse(buff)` to create a buffered response, sets the `Content-Type` header to `application/octet-stream` and the `Content-Length` header.
- `createStreamResponse(stream)` to create a streamed response, sets the `Content-Type` header to `application/octet-stream`.
- `createJsonResponse(object)` to create a JSON response, sets the `Content-Type` header to `application/json; charset=utf-8` and the `Content-Length` header.

```js
const { createServer, createTextResponse } = require('aldo-http')

// handler
const handler = () => createTextResponse("Hello world!")

// server
const server = createServer(handler)

// start
server.start(3000)
```

---
_Source: https://npm.io/package/aldo-http · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
