npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

brewski

v1.1.2

Published

A artisan web microframework

Downloads

3

Readme

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.