-
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);
});
-
-
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
}