pure-http

The simple web framework for Node.js with zero dependencies.

README

pure-http


Bring the middleware and router to native http.


Installation


  1. ```bash
  2. $ npm install --save pure-http
  3. ```

Usage


Basic server:

  1. ```js
  2. const pureHttp = require('pure-http');

  3. const app = pureHttp();

  4. app.get('/', (req, res) => {
  5.   res.send('Hello world');
  6. });

  7. app.listen(3000);
  8. ```

Existing server:

  1. ```js
  2. const http = require('http');
  3. const pureHttp = require('pure-http');

  4. const server = http.createServer();

  5. const app = pureHttp({ server });

  6. app.listen(3000);
  7. ```

Secure server:

  1. ```js
  2. const https = require('https');
  3. const pureHttp = require('pure-http');

  4. const server = https.createServer({
  5.   key: ...,
  6.   cert: ...,
  7. });

  8. const app = pureHttp({ server });

  9. app.listen(3000);
  10. ```

Application Options:


- server: Allows to optionally override the HTTP server instance to be used.

  > Default: undefined.

- onError: A handler when an error is thrown (Deprecated: It has been removed from 3.0.0).

  > Default: ((error, req, res) => res.send(error)).

- onNotFound: A handler when no route definitions were matched (Deprecated: It has been removed from 3.0.0).

  > Default: ((req, res) => res.send("Cannot " + req.method + " " + req.url)).

- views: An object to configuration render function.

  > Default: undefined.

  - dir: A directory for the application's views.

  - ext: The default engine extension to use when omitted.

  - engine: Registers the given template engine.

- Router Options:

  - prefix: Allow append the path before each route.

    > Default: undefined.

Router


  1. ```js
  2. const { Router } = require('pure-http');

  3. const router = Router();

  4. router.get('/', (req, res) => {
  5.   res.send('Hello world');
  6. });

  7. /* ... */

  8. const pureHttp = require('pure-http');

  9. const app = pureHttp();

  10. app.use('/api', router);

  11. app.listen(3000);
  12. ```

API References


You can read more at API.md.

Benchmarks


Please remember that your application code is most likely the slowest part of your application!

Switching from Express to pure-http will (likely) not guarantee the same performance gains.


- Machine: Ubuntu-s-1vcpu-1gb-sgp1-01, x86-64, Ubuntu 18.04.5 LTS, Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz, 16GB RAM.
- Node: v12.18.4
- Run: Tue, 16 Mar 2021 16:09:01

FrameworkVersionRequests/SecLatency
-----------------------------------::----------:----------:
**pure-http**latest****\~**15.11ms**
pure-httplatest~15.39ms
tinyhttp1.2.17~19.44ms
fastify3.14.0~29.10ms
express4.17.1~43.87ms

- Machine: Ubuntu-s-1vcpu-1gb-sgp1-01, x86-64, Ubuntu 18.04.5 LTS, Intel(R) Xeon(R) CPU E5-2650 v4 @ 2.20GHz, 16GB RAM.
- Node: v12.18.4
- Run: Fri, 13 Nov 2020 21:07:21

FrameworkVersionRequests/SecLatency
----------------------------------::----------:----------:
**pure-http**2.x.x****\~**10.92ms**
pure-http2.x.x~11.12ms
polka0.5.2~13.03ms
express4.17.1~26.86ms
fastify3.8.0~35.54ms