Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

brewski

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

brewski

A artisan web microframework

latest
Source
npmnpm
Version
1.1.2
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source
logo

brewski

A artisan web microframework

npm package version npm downloads standard JS linter prettier code formatting travis ci build status project license make a pull request


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!

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

    Or open up a issue.

    License

    Licensed under the MIT License.

    FAQs

    Package last updated on 18 Oct 2017

    Did you know?

    Socket

    Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

    Install

    Related posts