brewski
A artisan web microframework
Table of Contents
Table of Contents
About
Features
Install
Usage
API
Benchmark
Acknowledgements
Contribute
License
About
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
$ 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()
})
app.get('/', (req, res) => {
res.send('Hi from brewski')
})
app.get('/json', (req, res) => {
res.send({ test: true })
})
app.get('/status/:name', (req, res) => {
res.status(201).send({ name: req.params.name })
})
app.get('/query', (req, res) => {
res.send({ name: req.query.name })
})
app.get('/html', (req, res) => {
res.html('<h1>Hi</h1>')
})
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:
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()
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:
yarn && yarn benchmark
Results from latest benchmark (Mid 2015MacBook Pro, 2.2 GHz Intel Core i7, 16 GB 1600 MHz DDR3):
Stat Avg Stdev Max
Latency (ms) 15.71 3.36 193
Req/Sec 12377.87 878.23 13407
Bytes/Sec 1.79 MB 121 kB 1.97 MB
371k requests in 30s, 53.8 MB read
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.