Class: kawapp

kawapp

new kawapp()

Application class.
Source:

Classes

mw
response
util

Members

<static> END :Object

A signature to terminate the middleware sequence.
Type:
  • Object
Source:
Example
var app = kawapp();

app.use(function(context, canvas, next) {
  next(kawapp.END); // send END signal to stop the application
});

app.use(other_mw);  // this middleware will never be invoked

<static> SKIP :Object

A signature to skip the middleware sequence.
Type:
  • Object
Source:
Example
var sub = kawapp();
sub.use(function(context, canvas, next) {
  next(kawapp.SKIP);
});
sub.use(mw1); // this middleware will never be invoked

var main = kawapp();
main.use(sub);
main.use(mw2); // this middleware will be invoked otherwise

END :Object

Alias to `kawapp.END`.
Type:
  • Object
Source:

length :Number

Number of middlewares installed. This means kawapp instance behaves as an Array-like object.
Type:
  • Number
Source:

mw :kawapp.mw

Alias to `kawapp.mw`.
Type:
Source:

SKIP :Object

Alias to `kawapp.SKIP`.
Type:
  • Object
Source:

util :kawapp.util

Alias to `kawapp.util`.
Type:
Source:

Methods

mount(path, mw) → {kawapp}

This installs middleware(s) which are invoked when `location.pathname` matches.
Parameters:
Name Type Argument Description
path String | RegExp pathname to test
mw function <repeatable>
Middleware(s) to install
Source:
Returns:
Type
kawapp
Example
var app = kawapp();

app.mount("/about/", about_mw);        // test pathname with string

app.mount(/^\/contact\//, contact_mw); // test pathname with regexp

app.mount("/detail/", mw1, mw2, mw3);  // multiple middlewares to install

start(context, canvas, callback) → {kawapp}

This invokes a kawapp application.
Parameters:
Name Type Argument Description
context Object <optional>
request context object a.k.a. `locals`
canvas response | jQuery | cheerio <optional>
response element such as jQuery object
callback function <optional>
callback function
Source:
Returns:
Type
kawapp
Example
var app = kawapp();
app.use(mw1);              // install some middlewares

var context = {};          // plain object as a request context
var canvas = $("#canvas"); // jQuery object as a response canvas

app.start(context, canvas, function(err, canvas) {
  if (err) console.error(err);
});

use(mw) → {kawapp}

This installs middleware(s).
Parameters:
Name Type Argument Description
mw function <repeatable>
Middleware(s) to install
Source:
Returns:
Type
kawapp
Example
var app = kawapp();

app.use(function(context, canvas, next) {
  var text = context.ok ? "OK" : "NG"; // use context as a locals
  canvas.append(text);                // use canvas with append() html() empty() methods
  next();                          // callback to chain middlewares
});

app.use(function(context, canvas, next) {
  var err = new Error("something wrong");
  next(err);                       // send error to terminate the application
});

useif(cond, mw) → {kawapp}

This installs middleware(s) which are invoked when conditional function returns true.
Parameters:
Name Type Argument Description
cond function Conditional function
mw function <repeatable>
Middleware(s) to install
Source:
Returns:
Type
kawapp
Example
var app = kawapp();

app.useif(test, mw1, mw2);     // mw1&mw2 will be invoked only when condition is true

app.use(mw3, mw4);             // mw3&mw4 will be invoked only when condition is false

function test(context, canvas) {
  return (context.key == "value"); // test something
}