High performance static server cluster plugin for asimov.js
A high performance static server cluster plugin for asimov.js
asimov-server is a high-performance static server based on express.js. It uses a cluster of workers to serve a static site, built with asimov-pages or any other way, and allows you to customize the request/response flow with super-charged express.js middleware.
First things first - create a new, basic project. Then install asimov-server from npm.
$ npm install --save asimov-server
To use the server plugin, add it in your app's plugin hook, in
index.js and start your app with
var asimov = require'asimov';var server = require'asimov-server';// Keeping all our setup in this format allows our// app to be used as a plugin by other projectsmoduleexports =asimovuseserver;;// The project bootstrap, using our app as a pluginmoduleexportsstart =asimovusemoduleexportsstart;;// If we are not loaded as a plugin, start the appmoduleparent || moduleexportsstart;
asimovconfig// the folder where the static content is located'server.sourceDir': 'public'// seconds, how often request counts are logged'server.logInterval': 15// seconds, how often workers report request counts'server.workerReportInterval': 5
Create or use regular express.js middleware.
// lib/middleware/myMiddleware.jsmoduleexports =// do somenext;
Then include and register the middleware in your project's plugin hook.
// index.jsvar myMiddleware = require'./lib/middleware/myMiddleware';moduleexports =asimovmiddlewaremyMiddleware;;
Unlike in vanilla express.js app, it's possible to hook into several different steps in the request handling lifecycle.
Pre-middleware is executed before any other middleware and could be used to override the entire normal request lifecycle and middleware chain.
var overrideEverything = require'overrideEverything';asimovpremiddlewareoverrideEverything;
With post-middleware, you also hook in middleware right after the server has tried to serve content from the static source folder, if no content was found. With this you could, for example, add a custom logger to keep track of the amount of served 404s.
var myCustom404Logger = require'myCustom404Logger';asimovpostmiddlewaremyCustom404Logger;
Beware, when the pre-middleware is executed, the response will not have been equipped with any caching headers yet, so if you send a response, you might want to take of that on your own.
Author: Adam Renklint.