hyperserv
A hypermodular http server that glues together http, stack and @bret/http-hash-router.
npm i hyperserv
Why?
Express is a reliable and widely understood web-framework, but the dream of node.js was framework free network applicaitons. http-framework and substack-flavored-webapp are excellent counterpoints to frameworks like express and hapi but come along with a pile of boilerplate. hyperserv aims to glue together the basics of any webserver by providing a routing layer and a middlware layer to offer up a quick way to write small webservers the hypermodular way (or, more specifically, one hypermodular way)!
How you launch and configure your webservers seems to be a deeply personal ceremony. hyperserv leaves this up to you and just puts together the webserver for you.
Usage
var minimist = var morgan = var Hyperserv = var app = var argv = var ecstatic = var path = processtitle = 'hyperserv' // Reconfigure the middlewre stack sitting in front of the routes.app var staticPath = pathconsoleapprouter // Set up routesapprouter // Set up routesapprouter // Routes can fly fast and loose. It don't matterapprouter { res res} approuter apphttpServer
API
Routes vs Layers No More!
Hyperserv now supports connect style routes out of the box. The opts
object that holds params and app level vars are now extended into the req.opts
object by default, when the route doesn't accept an opts
argument.
var app = new Hyperserv([options])
Returns a new hyperserv app
object. It sets up an httpServer that has a middleware handler and router.
Default options:
layers: 'dev' sendTraces: true logTraces: true logDetails: true
layers
: Provide an array of middleware functions (function layer (req, res, cb) {}
) that get stuck in front of the final routing layer. You can reconfigure this layer at any point withserver.composeStack
.sendTraces
: Specify if stack traces are sent in theres
if thereq
runs into any kind of error. Defaults tofalse
logTraces
: Attach the default error handler to theerror
event emitted from the server whenever it encounters an error.
{ if errstatusCode !== 404 console}
logDetails
: Attach the default server start log message to the server to fire when it starts listening.
app.httpServer
This is an instance of http.createServer
. It isn't started yet, so set up your event handlers, and turn it on with app.httpServer.listen(port)
app.router
This is the @bret/http-hash-router
router object that has simply been attached to the http
server instance. Read all about it here:
app.router.set(pattern, routeHandler)
This sets a route and a route handler. Remember, routeHandlers expect the following signature function route (req, res, opts, cb) {}
. You can compose middleware stack's to plop inside of route handlers using stack.compose
.
See bcomnes/http-hash-router#example
app.composeStack([ layers ])
This lets you pass an array of middleware layers (function layer (req, res, cb) {}
) to stick in front of the http-hash-router
layer. You can do body parsing, cookie parsing, sessions, and auth stuff here. Calling composeStack
tosses the existing middleware stack out in favor of the one you pass in here.
app.errorHandler
This is the default error handler that gets passed to the server's error
event when logTraces
is set to true when creating a server
. It is an instance method that you can reassign if you want.
{ if errstatusCode !== 404 console}
If you want to use it as is with the server, use the logTraces
option. It is exported only for convince and access and should not be normally used directly.
app.logDetails
This is the default logging function that runs when the server starts listening. Use the logDetails
options to turn it on or off. It is an instance method that you can reassign if you want.
Hyperserv.makeRoute(layer)
(Depreciated)
Pass in a connect style middleware layer and get back a http-hash-router
route handler. The returned route handler mixes in any options it receives on its opts
argument to req.opts
.
{ return { reqopts = }}