# http-mw

> Bare minimum middleware for node's built-in http server module

Latest version **0.0.12** (published 2024-10-22) · ISC license · 0 weekly downloads

## Install

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

## Health

**Score 25/100 (F)** — status: maintenance-mode.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2024-10-22 |
| First published | 2024-08-27 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 35.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | PsyChip |
| Maintainers | psychip |
| Keywords | http, middleware, static server, resource server, microservice, performance, webserver, www, minimal server, http middleware, user layer, microservices, security, file uploads, payload parser, query parser, sessions, client uid, session persistance |

## Links

- npm: https://www.npmjs.com/package/http-mw
- Homepage: https://psychip.net
- npm.io page: https://npm.io/package/http-mw

## Dependencies (3)

- [cookie](https://npm.io/package/cookie.md) ^0.6.0
- [formidable](https://npm.io/package/formidable.md) ^3.5.1
- [query-string](https://npm.io/package/query-string.md) ^7.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 0.0.12 (latest) — 2024-10-22
- 0.0.11 — 2024-09-08
- 0.0.10 — 2024-09-08
- 0.0.9 — 2024-08-30
- 0.0.8 — 2024-08-28
- 0.0.7 — 2024-08-28
- 0.0.6 — 2024-08-28
- 0.0.5 — 2024-08-28
- 0.0.4 — 2024-08-28
- 0.0.3 — 2024-08-28
- 0.0.2 — 2024-08-28
- 0.0.1 — 2024-08-27

## README

## http-mw
Bare minimum middleware for node's built-in http server module

### Features
- Parses incoming form data on each POST request, supports modern payload types. 
- Parses query parameters if exists
- Accepts multiple file uploads.
- Built-in static web server.
- Basic http security, filters out the abusive clients.
- Session support, assigns a persistent id to every client.
- Automatic mime type detection.
- CORS friendly.

### Installation
```sh
$ npm i http-mw
```

### Usage
```js
const server = require("http-mw");

server.listen({
    "port": 8080,
    "webroot": "./public",
    "uploads": "./files"
}, function (req) {
    switch (req.method) {
        case "get":
            switch (req.path[0]) {
                // api endpoint
                case "info":
                    req.send({
                        "ip": req.ip,
                        "sid": req.sid,
                        "params": req.params,
                        "cookies": req.cookies
                    });
                    break;
                    // string type detection test
                case "test":
                    req.send('<html><body><h1>It Works!</h1></body></html>');
                    break;
                    // redirect test
                case "redirect":
                    req.redirect("http://google.com/");
                    break;
                    // respond with error
                case "error":
                    req.deny("You don't have access to this resource", 401);
                    break;
                default:
                    // serve static files from webroot dir
                    req.static();
            }
            break;
        case "post":
        switch (req.path[0]) {
            case "info":
                req.send({
                    "ip": req.ip,
                    "sid": req.sid,
                    "payload": req.payload,
                    "files": req.files
                });
                break;
            default:
                req.deny("undefined endpoint", 404);
        }
    }
}, function (conf) {
    console.log("-- server running on port " + conf.port);
    console.log("-- middleware test url: http://localhost:" + conf.port + "/info");
});
```

### Documentation

Not available yet

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