Code Monkey home page Code Monkey logo

0http's Introduction

0http

NPM version

Cero friction HTTP framework:

  • Tweaked Node.js Server for high throughput.
  • Use the request router you like.

Performance Benchmarks

You can read more about this numbers here.

Usage

const cero = require('0http')
const { router, server } = cero()

router.get('/hello', (req, res) => {
  res.end('Hello World!')
})

router.post('/do', (req, res) => {
  // ...
  res.statusCode = 201
  res.end()
})

//...

server.listen(3000)

Routers

0http allows you to define the router implementation you prefer as soon as it support the following interface:

router.lookup = (req, res) // -> should trigger router search and handlers execution

0http - sequential (default router)

This a 0http extended implementation of the trouter router. Includes support for middlewares, nested routers and shortcuts for routes registration.
As this is an iterative regular expression matching router, it tends to be slower than find-my-way when the number of registered routes increases; to mitigate this issue, we use an internal(optional) LRU cache to store the matching results of the previous requests, resulting on a super-fast matching process.

Supported HTTP verbs: GET, HEAD, PATCH, OPTIONS, CONNECT, DELETE, TRACE, POST, PUT

const cero = require('0http')
const { router, server } = cero({})

// global middleware example
router.use('/', (req, res, next) => {
  res.write('Hello ')
  next()
})

// route middleware example
const routeMiddleware = (req, res, next) => {
  res.write('World')
  next()
}

// GET /sayhi route with middleware and handler
router.get('/sayhi', routeMiddleware, (req, res) => {
  res.end('!')
})

server.listen(3000)

Configuration Options

  • defaultRoute: Route handler when there is no router matching. Default value:
    (req, res) => {
      res.statusCode = 404
      res.end()
    }
  • cacheSize: Router matching LRU cache size. A given value <= 0 will disable the cache. Default value: 1000
  • errorHandler: Global error handler function. Default value:
    (err, req, res) => {
      res.statusCode = 500
      res.end(err.message)
    }

Example passing configuration options:

const sequential = require('0http/lib/router/sequential')
const { router, server } = cero({
  router: sequential({
    cacheSize: 2000
  })
})

Async middlewares

You can use async middlewares to await the remaining chain execution. Let's describe with a custom error handler middleware:

router.use('/', async (req, res, next) => {
  try {
    await next()
  } catch (err) {
    res.statusCode = 500
    res.end(err.message)
  }
})

router.get('/sayhi', (req, res) => {
  throw new Error('Uuuups!')
})

Nested Routers

You can simply use sequential router intances as nested routers:

const cero = require('../index')
const { router, server } = cero({})

const nested = require('0http/lib/router/sequential')()
nested.get('/url', (req, res, next) => {
  res.end(req.url)      
})
router.use('/v1', nested)

server.listen(3000)

find-my-way router

https://github.com/delvedor/find-my-way

Super-fast raw HTTP router with no goodies. Internally uses a Radix Tree router that will bring better performance over iterative regular expressions matching.

const cero = require('../index')
const { router, server } = cero({
  router: require('find-my-way')()
})

router.on('GET', '/hi', (req, res) => {
  res.end('Hello World!')
})

server.listen(3000)

Servers

0http is just a wrapper for the servers and routers implementations you provide.

const cero = require('0http')

const { router, server } = cero({
  server: yourCustomServerInstance
})

Node.js http.Server

If no server is provided by configuration, the standard Node.js http.Server implementation is used.
Because this server offers the best balance between Node.js ecosystem compatibility and performance, we highly recommend it for most use cases.

Benchmarks (30/12/2019)

Node version: v12.14.0
Laptop: MacBook Pro 2019, 2,4 GHz Intel Core i9, 32 GB 2400 MHz DDR4
Server: Single instance

wrk -t8 -c40 -d5s http://127.0.0.1:3000/hi

1 route registered

  • 0http (sequential)
    Requests/sec: 88438.69
  • 0http (find-my-way)
    Requests/sec: 87597.44
  • restana v3.4.2
    Requests/sec: 73455.97

5 routes registered

  • 0http (sequential)
    Requests/sec: 85839.17
  • 0http (find-my-way)
    Requests/sec: 82682.86

For more accurate benchmarks please see:

Support / Donate ๐Ÿ’š

You can support the maintenance of this project:

  • Paypal: https://www.paypal.me/kyberneees
  • NANO Crypto Coin: nano_3zm9steh8mb374f8be3rbytqhgzzarczhwtxhihkqt83a4m46oa3xidfiauc
  • XRP Crypto Coin: rarQgNuiqF9gFLLwd5fdku4jYa9EXpiyCp
  • TRON Crypto Coin: TJ5Bbf9v4kpptnRsePXYDvnYcYrS5Tyxus
  • BITCOIN Crypto Coin: bc1qcrr58venyh54ztvkqym39p9rhnxg4308t0802f
  • Ethereum Crypto Coin: 0xD73c8E63a83eBD8Df3fB3d0090f1fe7a1eEB980B

Breaking Changes:

3.x

0http's People

Contributors

jesusvilla avatar jkyberneees avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.