asimov-server v1.5.0
asimov-server
A high performance static server cluster plugin for asimov.js
Made by Adam Renklint, Berlin 2014. MIT licensed.
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.
Getting started
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 asimov
or node index.js
.
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 projects
module.exports = function plugin (options) {
asimov.use(server);
};
// The project bootstrap, using our app as a plugin
module.exports.start = function bootstrap () {
asimov
.use(module.exports)
.start();
};
// If we are not loaded as a plugin, start the app
module.parent || module.exports.start();
Configuration
asimov.config({
// 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
})
Middleware
Create or use regular express.js middleware.
// lib/middleware/myMiddleware.js
module.exports = function myMiddleware (req, res, next) {
// do some
next()
};
Then include and register the middleware in your project's plugin hook.
// index.js
var myMiddleware = require('./lib/middleware/myMiddleware');
module.exports = function plugin () {
asimov.middleware(myMiddleware);
};
Pre and post middleware
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');
asimov.premiddleware(overrideEverything);
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');
asimov.postmiddleware(myCustom404Logger);
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.
Develop and contribute
- First, fork this repo.
- Implement something awesome
- Write tests and run them with
npm test
- Submit a pull request
Credits
Author: Adam Renklint.