AdonisJS Http Server
Decently fast HTTP server used by AdonisJS
This module is extracted from the AdonisJS framework to work as a standalone HTTP server. The performance of the server is on par with Fastify (not as fast as fastify though).
Table of contents
Benchmarks
The benchmarking scheme is taken from the Fastify github repo.
Machine: Quad-Core Intel Core i7, 2.2GHz, 16GB RAM
Method: autocannon -c 100 -d 40 -p 10 localhost:3000 * 2, taking the second average
Framework | Version | Router? | Requests/sec |
---|
Fastify | 2.0.0 | ✓ | 52709 |
AdonisJs | 1.5.4 | ✓ | 47791 |
You can run the same benchmarks by cloning the repo and then running the following command.
npm run benchmark
Since the program correctness and reliability is more important over micro optimizations. We pay penality on following fronts in comparison to Fastify.
- The AdonisJS query string parser can parse arrays inside query string
(/api?foo[]=bar&foo[]=fuzz&foo[]=buzz )
, wherease fastify doesn't parse it by default for performance reasons. However, you can also define your own query string parser with fastify, but again, you will end up paying the same performance penality. - Subdomain based routing is another front, where AdonisJS has to perform little bit extra work to find the correct route and it's handler.
Features
- The most advanced router with support for route resources, route groups, subdomains routing.
- Support for global and route specific middleware.
- Reliable and stable query string parser.
- Global exception handler to catch all exceptions handled during an HTTP request.
- Sends data to AdonisJS inbuilt profiler
Usage
You must be using the server inside a fully fledged AdonisJS application. Still, here's how you can start the standlone server.
npm i @adonisjs/http-server
import proxyaddr from 'proxy-addr'
import { createServer } from 'http'
import { Ioc } from '@adonisjs/fold'
import { Logger } from '@adonisjs/logger/build/standalone'
import { Profiler } from '@adonisjs/profiler/build/standalone'
import { Encryption } from '@adonisjs/encryption/build/standalone'
import { Server } from '@adonisjs/http-server'
const logger = new Logger({ enabled: true, level: 'trace', name: 'adonis' })
const profiler = new Profiler({ enabled: true })
const encryption = new Encryption('averylongrandom32charslongsecret')
const server = new Server(new Ioc(), logger, profiler, encryption, {
etag: false,
jsonpCallbackName: 'callback',
cookie: {},
subdomainOffset: 2,
generateRequestId: false,
secret: Math.random().toFixed(36).substring(2, 38),
trustProxy: proxyaddr.compile('loopback'),
allowMethodSpoofing: false,
})
server.router.get('/', async () => {
return { hello: 'world' }
})
server.optimize()
createServer(server.handle.bind(server)).listen(4000)