brewski

1.1.2 • Public • Published
logo

brewski

A artisan web microframework



Built with ❤︎ by tiaanduplessis and contributors

Table of Contents

Table of Contents
  • About
  • Features
  • Install
  • Usage
  • API
  • Benchmark
  • Acknowledgements
  • Contribute
  • License
  • About

    Greenkeeper badge

    Brewski is a tiny node framework that assists in the creation of performant APIs when other options feel like overkill.

    Features

    • Fast: being perfomant is a must
    • Extendible: supports express style middleware
    • Logging: build in logger
    • Environment Management: simple handling of enviroment variables with envobj
    • Chainable: API is completely chainable

    Install

    $ npm install brewski
    # OR
    $ yarn add brewski

    Usage

    const brewski = require('brewski')
    
    const app = brewski({
      env: {
        port: 8888
      }
    })
    
    const { log, env } = app
    
    app.use((req, res, next) => {
      log.info({ url: req.url })
      next()
    })
    
    // Middleware with error
    // app.use((req, res, next) => {
    //   next(new Error('Should be error'))
    // })
    
    // http://localhost:8888
    app.get('/', (req, res) => {
      res.send('Hi from brewski')
    })
    
    // http://localhost:8888/json
    app.get('/json', (req, res) => {
      res.send({ test: true })
    })
    
    // http://localhost:8888/status/tiaan
    app.get('/status/:name', (req, res) => {
      res.status(201).send({ name: req.params.name })
    })
    
    // http://localhost:8888/query?name=Tiaan
    app.get('/query', (req, res) => {
      res.send({ name: req.query.name })
    })
    
    // http://localhost:8888/html
    app.get('/html', (req, res) => {
      res.html('<h1>Hi</h1>')
    })
    
    // http://localhost:8888/redirect
    app.get('/redirect', (req, res) => {
      res.redirect(`http://localhost:${app.env.port}/`)
    })
    
    app.listen(app.env.port)

    API

    brewski(options): Object

    Creates and returns a new brewski object.

    Options include:

    • logLevel: String - Level of logging for pino logger. Defaults to info.
    • logStream: Stream - Stream to log to. Defaults to process.stdout.
    • env: Object - Environment variables to pass to envobj.
    • https: Object - Configuration options if https server should be created. Uses same options as http.createServer
    • defaultHandler: function - Function to handle act as default route e.g.:
      function defaultHandler(req, res) {
        res.statusCode = 404
        res.end()
      }

    Example:

    const brewski = require('brewski')
    const api = brewski({
      logLevel: 'fatal',
      env: {
        port: 3000
      }
    })

    brewski.use(middleware: Function): Object

    Add new middleware to the stack. Middleware is processed in the order they are added. Since the middleware function matches that of express, existing packages can be used with no additional configuration:

    const brewski = require('brewski')
    const api = brewski()
    
    const helmet = require('helmet')
    
    api.use(helmet())

    brewski.route(method: String|Array, route: String, handler: Function)

    Creates a new route. Brewski uses find-my-way to handle routing and supports parametric, wildcard and parametric with regex routes e.g:

    const brewski = require('brewski')
    const api = brewski()
    
    api.get('/foo/:baz', (req, res) => {
      res.send(req.params)
    })
    
    api.get('/baz/*', (req, res) => {
      res.send(req.params)
    })
    
    api.get('/baz/:thing(^\\d+).png', (req, res) => {
      res.send(req.params)
    })

    Shorthand Methods

    You can also use the shorthand methods to declare your routes e.g:

    const brewski = require('brewski')
    const api = brewski()
    
    function handler(req, res) {
      res.send({success: true})
    }
    
    api.get('/foo', handler)
    api.post('/foo', handler)
    api.patch('/foo', handler)
    api.delete('/foo', handler)
    api.head('/foo', handler)
    api.put('/foo', handler)
    api.option('/foo', handler)
    
    api
      .get('/baz', handler)
      .post('/baz', handler)

    brewski.server

    Access to the HTTP server instance created by brewski. See node docs for more information.

    brewski.listen(port, address, callback)

    Starts the created server on provided port and address. If port is not provided, env.PORT is used. callback is called after server starts listening.

    Example:

    const brewski = require('brewski')
    const api = brewski()
    
    api.listen()
    // api.listen(3000)
    // api.listen(3000, () => console.log('Listening on port 3000'))

    brewski.log

    Provides access to the pino logger instance e.g:

    const brewski = require('brewski')
    const api = brewski()
    
    api.log.info({foo: true})

    See pino documentation for more options.

    brewski.env

    Provides access to envobj object.

    Benchmark

    Benchmark is run using autocannon with 200 concurrent connection for 30s. To run the benchmark:

    npm i && npm run benchmark:
    # Or
    yarn && yarn benchmark

    Results from latest benchmark (MacBook Air (13-inch, 2017), 1,8 GHz Intel Core i5, 8 GB 1600 MHz DDR3):

    Stat         Avg      Stdev   Max
    Latency (ms) 18.02    6.88    276
    Req/Sec      10863.34 1655.63 12607
    Bytes/Sec    1.58 MB  241 kB  1.84 MB
    
    326k requests in 30s,
    

    Acknowledgements

    This package is inspired and influenced by fastify.

    Contributing

    Contributions are welcome!

    1. Fork it.
    2. Create your feature branch: git checkout -b my-new-feature
    3. Commit your changes: git commit -am 'Add some feature'
    4. Push to the branch: git push origin my-new-feature
    5. Submit a pull request :D

    Or open up a issue.

    License

    Licensed under the MIT License.

    Readme

    Keywords

    none

    Package Sidebar

    Install

    npm i brewski

    Weekly Downloads

    0

    Version

    1.1.2

    License

    MIT

    Last publish

    Collaborators

    • tiaanduplessis