Merry is a little Node framework that helps you build performant applications
with little effort. We don't think that "fast" and "cute" should be mutually
exclusive. Out of the box we've included consistent logging, standardized error
handling, a clean streams API and plenty of nuts, bolts and options to
customize merry to fit your use case. We hope you have a good time using it.
:v: -Team Merry
Features
fast: using Node streams, merry handles request like no other
fun: helps with boring stuff like error handling
communicative: standardized ndjson logs for everything
sincere: doesn't monkey patch Node's built-ins
linear: smooth sailing from tinkering to production
var merry = require('merry')var app = merry()app.route('GET', '/', function (req, res, ctx) { ctx.log.info('oh hey, a request here') ctx.send(200, { cute: 'butts' })})app.route('default', function (req, res, ctx) { ctx.log.info('Route doesnt exist') ctx.send(404, { message: 'nada butts here' })})app.listen(8080)
$ node index.js | merry
Logging
Merry uses the pino logger under the hood. When you create a new merry app,
we enable a log forwarder that by default prints all logs to process.stdout.
There are different log levels that can be used. The possible log levels are:
debug: used for developer annotation only, should not be enable in
production
info: used for transactional messages
warn: used for expected errors
error: used for unexpected errors
fatal: used for critical errors that should terminate the process
The difference between an expected and unexpected error is that the first is
generally caused by a user (e.g. wrong password) and the system knows how to
respond, and the latter is caused by the system (e.g. there's no database) and
the system doesn't know how to handle it.
Error handling
Error handling is different for each application. Errors come in different
shapes, have different status codes, so we can't provide a one-size-fits-all
solution. But we do think that having consistent error messages is useful, so
Merry comes with a recommended pattern to handle errors.
// index.jsvar errors = require('./errors')var merry = require('merry')var db = require('my-cool-db')var app = merry()app.route('GET', '/', function (req, res, ctx) { db.get('some-key-from-request', function (err, data) { if (err) return errors.ENOTFOUND(req, res, ctx) ctx.send(200, data) })})app.listen(8080)
Configuration
Generally there are two ways of passing configuration into an application.
Through files and through command line arguments. In practice it turns out
passing environment variables can be done with less friction than using files.
Especially in siloed environments such as Docker and Kubernetes where mounting
volumes can at times be tricky, but passing environment variables is trivial.
Merry ships with an environment argument validator that checks the type of
argument passed in, and optionally falls back to a default if no value is
passed in. To set the (very common) $PORT variable to default to 8080 do:
Merry uses server-router under the hood to create its routes. Routes are
created using recursive arrays that are turned into an efficient trie
structure under the hood. You don't need to worry about any of this though; all
you need to know is that we've tested it and it's probably among the fastest
methods out there. Routes look like this:
Partial routes can be set using the ':' delimiter. Any route that's
registered in this was will be passed to the ctx argument as a key. So
given a route of /foo/:bar and we call it with /foo/hello, it will show up
in ctx as { bar: 'hello' }.
Middleware
We do provide you with a way to access your route's ctx via app.use
For http2 support you will need to provide a
key and a cert to establish a secure connection. These can be passed as
part of merry's opts. If http2 is not available, an error will be thrown to
upgrade your node version to > v8.4.0.