# http-micro

> Micro-framework on top of node's http module

Latest version **3.2.0** (published 2017-07-14) · Apache-2.0 license · 0 weekly downloads

## Install

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

## 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 | 3.2.0 |
| Published | 2017-07-14 |
| First published | 2017-04-18 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 19 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Prasanna V. Loganathar |
| Maintainers | prasannavl |
| Keywords | server, http, micro-framework |

## Links

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

## Dependencies (19)

- [debug](https://npm.io/package/debug.md) ^2.6.3
- [accepts](https://npm.io/package/accepts.md) ^1.3.3
- [type-is](https://npm.io/package/type-is.md) ^1.6.15
- [raw-body](https://npm.io/package/raw-body.md) ^2.2.0
- [mime-types](https://npm.io/package/mime-types.md) ^2.1.15
- [@types/node](https://npm.io/package/@types/node.md) ^8.0.10
- [http-errors](https://npm.io/package/http-errors.md) ^1.6.1
- [on-finished](https://npm.io/package/on-finished.md) ^2.3.0
- [@types/debug](https://npm.io/package/@types/debug.md) 0.0.29
- [content-type](https://npm.io/package/content-type.md) ^1.0.2
- [@types/accepts](https://npm.io/package/@types/accepts.md) ^1.3.2
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^1.7.0
- [@types/raw-body](https://npm.io/package/@types/raw-body.md) ^2.1.4
- [@types/mime-types](https://npm.io/package/@types/mime-types.md) ^2.1.0
- [@types/http-errors](https://npm.io/package/@types/http-errors.md) ^1.5.34
- [@types/on-finished](https://npm.io/package/@types/on-finished.md) ^2.2.29
- [@types/content-type](https://npm.io/package/@types/content-type.md) ^1.1.0
- [content-disposition](https://npm.io/package/content-disposition.md) ^0.5.2
- [@types/content-disposition](https://npm.io/package/@types/content-disposition.md) ^0.5.2

## Recent versions

- 3.2.0 (latest) — 2017-07-14
- 3.1.0 — 2017-07-12
- 3.0.0 — 2017-07-11
- 2.0.1 — 2017-07-09
- 1.13.1 — 2017-06-13
- 1.13.0 — 2017-06-09
- 1.12.0 — 2017-05-31
- 1.11.0 — 2017-05-30
- 1.10.0 — 2017-05-29
- 1.9.4 — 2017-05-28
- 1.9.3 — 2017-05-27
- 1.9.2 — 2017-05-27
- 1.9.1 — 2017-05-27
- 1.9.0 — 2017-05-25
- 1.8.4 — 2017-05-25
- … 22 more at https://npm.io/package/http-micro/versions

## README

# http-micro

Micro-framework on top of node's http module

```js
app.use((ctx) => {
    ctx.res.end("Hello world!");
    return Promise.resolve();
});
```

**Installation:** `npm install http-micro`

### Highlights

- Written in and works with Typescipt.
- Koa-like contexts that can be generically typed in TS.
- Promises for all middleware.
- In-built body-parsing - but only ever parse the body if you need it,
  providing better security and performance.
- In-built graceful server shutdown with proper keep-alive handling.
- No monkey-patching Node's http module or any other module.
- Provides middleware chaining and composition over core `http` module.
- Awaitable `next` middlewares. So, parent middleware has complete
  control over the execution of the next middleware.
- Just use `async/await` and wrap in `try-catch` for error handling. No 
  special quirks or ugly special-case callbacks like Express.
- High performance (Combined with bluebird promises,
  performance is on par with node's raw http module).
- Small code-base.

### Example

```js
const micro = require("http-micro");
const url = require("url");

let app = new micro.App();
// When using Typescript, context can be 
// generically typed to one that implements
// the IContext interface.
//
// `let app = new micro.Application<MyContext>(
//          (app, req, res) => new MyContext());`
//
// Raw node req, and res untouched.
// Convenience functions are also provided, used
// later in the example.

app.use(async (ctx, next) => {
    if (url.parse(ctx.req.url)
        .pathname == "/async") {
        ctx.res.end("Hello world from async!");
    } else {
        await next();
    }
});

app.use(async (ctx, next) => {
    await next();
})

app.use(async (ctx, next) => {
    if (url.parse(ctx.req.url)
        .pathname == "/async-await") {
        await Promise.resolve();
        await Promise.resolve();
        ctx.res.end("Hello world from awaited async!");
    } else {
        await next();
    }
});

let router = new micro.Router();
// When using Typescript, can again be generic,
// `let router = new Router<MyContext>();`

router.get("/hello", (ctx) => {
    ctx.sendText("Hello route!");
    return Promise.resolve();
});

router.get("/hello-string", async (ctx) => {
    ctx.sendText("Hello string!");
});

router.get("/hello-object", (ctx) => {
    ctx.sendAsJson({ message: "Hello world!" });
    return Promise.resolve();
});

let router1 = new micro.Router();

router1.get("/hello", (ctx) => {
    ctx.sendText("chain 1: hello!");
    return Promise.resolve();
});

let router2 = new micro.Router();

router2.get("/hello", (ctx) => {
    ctx.res.end("chain 2: hello!");
    return Promise.resolve();
});

router.use("/r1", router1);
router.use("/r2", router2);

app.use(router);

app.listen(8000, "localhost", () => {
    console.log("listening");
});
```

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