Git Product home page Git Product logo

kraken_example_custom_middleware's Introduction

Kraken Example: Custom Middleware

An application to show the use of custom middleware for kraken.

What we'll be doing

Starting with a plain vanilla application, we're going to create a simple page counter (as middleware) and configure the application to use it.

Running this example

Clone this repo: git clone https://github.com/lensam69/Kraken_Example_Custom_Middleware.git

Install the dependencies: npm install

Start the server: npm start

Relevant code

Application entry point

./index.js has four hooks where you can customize your application. These will be called exactly once when your application starts up.

  • app.configure is the first one to be called. It will receive the parsed data from the configuration files in ./config/
  • app.requestStart is called before the default middleware is loaded (See appcore.js)
  • app.requestBeforeRoute is called after middleware is loaded, and before the routes are created.
  • app.requestAfterRoute is the final call, and it takes place after all routes have been created.

Changing the code

Writing custom middleware

For this example we'll create a very rudimentary counter to find out how many requests we've served (In millions). It will be deployed as middleware, so it will be invoked every time a resource is loaded. If you've never written middleware, read this excellent tutorial for an explanation on how middleware works.

Our middleware will reside in the ./lib/ directory, and will be called millionsServed.js

See change here

module.exports = function () {
    var requestsServed = 0;

    return function (req, res, next) {
        requestsServed += 1;
        console.log(requestsServed / 1000000 + ' Million Pages Served!');
        next();
    };
};

This function will be invoked for every request to the server. Since we load things like scripts and images along with the page, you may see this invoked multiple times per page load. Every time a request is received, we increment our counter by one and print to the console

Loading the middleware

Now that we've written the module, it's time to load it during the application startup. First, we need to require the module we just created. Then we're going to modify the requestBeforeRoute function to load our middleware after the default modules have been loaded, and before the routes have been mounted.

See change here

app.requestStart = function requestStart(server) {
    server.use(millionsServed());
};

Since kraken is built on top of express, server is nothing more than the express app, and can be configured as usual.

Running the example

Start your server npm start and visit a page http://localhost:8000

You should see the counter output in the console: 0.000001 Million Pages Served!

kraken_example_custom_middleware's People

Contributors

lensam69 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.