# http-request-handler

> A tiny library for parsing and handling HTTP requests.

Latest version **2.0.1** (published 2022-06-29) · MIT license · 0 weekly downloads

## Install

```sh
npm install http-request-handler
pnpm add http-request-handler
yarn add http-request-handler
bun add http-request-handler
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.1 |
| Published | 2022-06-29 |
| First published | 2022-03-24 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 86.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Daniel Knerr |
| Maintainers | knerr |
| Keywords | http, requests, routing, server |

## Links

- npm: https://www.npmjs.com/package/http-request-handler
- Repository: https://github.com/DanielKnerr/http-request-handler
- Issues: https://github.com/DanielKnerr/http-request-handler/issues
- npm.io page: https://npm.io/package/http-request-handler

## Dependencies (4)

- [pug](https://npm.io/package/pug.md) ^3.0.2
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.35
- [picocolors](https://npm.io/package/picocolors.md) ^1.0.0
- [@types/mime-types](https://npm.io/package/@types/mime-types.md) ^2.1.1

## 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

- 2.0.1 (latest) — 2022-06-29
- 2.0.0 — 2022-06-29
- 1.1.0 — 2022-04-09
- 1.0.1 — 2022-03-24
- 1.0.0 — 2022-03-24

## README

# `http-request-handler`
`http-request-handler` is a tiny library for parsing and handling HTTP-requests in `Node.js`.

## Installation
`npm install http-request-handler`

## Usage
### Basics
Create a new `HTTPRequestHandler` object, declare your routes and then use the `getListener()` function to start the server:
```js
const {HTTPRequestHandler, HttpMethod} = require("http-request-handler");
const http = require("http");

let handler = new HTTPRequestHandler({
    endpoint: "/myapi"
});

handler.on(HttpMethod.HTTP_GET, "/hello", [], (request, response) => {
    response.send("world");
});

http.createServer(handler.getListener()).listen(3000);

// visit http://localhost:3000/myapi/hello
```

### Middleware
Middleware functions can preprocess requests (for example by attaching custom data) before they are passed on to the route handler. They can also reject or respond to requests. The middleware functions of a route will be executed in the order they are listed until a middleware function sends a response, in which case neither the remaining middleware functions or the route handler will be called.
```js

// This middleware function rejects all requests that don't have a valid "token" cookie.
const isUserLoggedIn = (request, response, data, resolve, reject) => {
    let valid = checkCookie(request.cookies.token);

    // allow this request to be passed to the next middleware function
    if (valid) resolve(); 
    else reject(403); // 403 Forbidden
};

// This middleware function assumes that the "token" cookie is valid. It will attach a "userID" property to the data object containing the ID of the logged in user.
const getUserID = (request, response, data, resolve, reject) => {
    let userID = getUserID(request.cookies.token);
    resolve({
        userID: userID
    });
};

handler.on(HttpMethod.HTTP_POST, "/upload", [isUserLoggedIn, getUserID], (request, response, data) => {
    // => this request comes from a logged in user with the ID data.userID
});
```

### HTTPS
Since `http-request-handler` isn't a server, you can setup HTTPS the default way:
```js
const {HTTPRequestHandler, HttpMethod} = require("http-request-handler");
const http = require("http");
const https = require("https");
const fs = require("fs");

const httpsOptions = {
    key: fs.readFileSync("keyFile.pem"),
    cert: fs.readFileSync("certFile.pem")
};

let handler = new HTTPRequestHandler({
    endpoint: "/myapi"
});

handler.on(HttpMethod.HTTP_GET, "/hello", [], (request, response) => {
    response.send("world");
});

https.createServer(httpsOptions, handler.getListener()).listen(3443);

// use HTTPRequestHandler.redirectToHTTPS to redirect all traffic to HTTPS
http.createServer(handler.redirectToHTTPS()).listen(3000);
```

## Development
- to run the tests run `npm test`
- to compile TypeScript to JavaScript run `npm run prepublish`

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