microrest v0.8.1
microrest
Extremely small, extremely fast REST framework for when size and speed matter. Also perfect for embedding a web API into an existing app.
To use as a bare-bones request handler (rest):
const rest = require('microrest');
const app = rest((req, res, next) => {
// request body is in the req.body Buffer
res.end();
next();
}
http.listen(0, (err, serverInfo) => {
// app is listening on port `serverInfo.port`
});
To use as a light-weight app (rest_ha):
const rest = require('microrest');
const app = rest();
app.get('/hello', (req, res, next) => {
res.end('hi back');
next();
})
const server = app.listen(1337);
To use as a fully routed app with middleware steps (rest_mw):
const rest = require('microrest');
const mw = require('microrest/mw');
const Router = require('microrest/router');
const app = rest({ router: new Router() });
app.use(mw.mwParseQuery);
app.use(mw.mwReadBody);
app.get('/hello/:arg1/:arg2', (req, res, next) => {
// request body available in req.body
// route and query params available in req.params
res.end();
next();
})
app.listen(1337);
To embed, copy rest.js
(and possibly also mw.js and router.js) into your own library,
and use as an internal component.
Documentation
Detailed documenation is in the manual
Benchmark
Requests served per second in batches of 100 concurrent calls of a 20 byte request, 200 byte response, calls made by nodejs using a keepAlive Agent with default maxSockets:
qtimeit=0.21.0 node=8.11.1 v8=6.2.414.50 platform=linux kernel=4.9.0-0.bpo.4-amd64 up_threshold=false
arch=ia32 mhz=4383 cpuCount=4 cpu="Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz"
name speed rate
restify 12,194 ops/sec 1000 >>>>>
express 16,344 ops/sec 1340 >>>>>>>
rest_ha 34,505 ops/sec 2830 >>>>>>>>>>>>>>
rest 34,794 ops/sec 2853 >>>>>>>>>>>>>>
http 28,980 ops/sec 2377 >>>>>>>>>>>>
And, just for fun, a fast non-REST remote procedure call library (single socket):
qrpc 131,703 ops/sec 10800 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
With the test load generated externally to nodejs by wrk
:
# wrk -d2s -t2 -c50 http://localhost:1337/test1
restify: 14957.77 xxxxxxx
express: 23179.38 xxxxxxxxxxxx
rest_mw: 80719.16 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
rest_ha: 83304.33 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
rest: 89870.42 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
http: 57735.86 xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Testing
To keep the size small, the npm package does not in include the tests. To run the tests or benchmarks, check out the repo from https://github.com/andrasq/node-microrest.