From 569e6723333d0253b38e061297639271d5e83100 Mon Sep 17 00:00:00 2001 From: brettlangdon Date: Sat, 31 Aug 2013 08:54:42 -0400 Subject: [PATCH] add a simple example --- examples/simple/app.js | 12 ++++++++++ examples/simple/my_plugin.js | 45 ++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 examples/simple/app.js create mode 100644 examples/simple/my_plugin.js diff --git a/examples/simple/app.js b/examples/simple/app.js new file mode 100644 index 0000000..e85ebf6 --- /dev/null +++ b/examples/simple/app.js @@ -0,0 +1,12 @@ +var yaps = require("../../"); + +var my_plugin = require("./my_plugin.js"); + +var app = new yaps.server({ + some: "settings", + that: "get passed into each plugin on construct", +}); + +app.registerPlugin(my_plugin); +// listening on default 0.0.0.0:8000 +app.start(); diff --git a/examples/simple/my_plugin.js b/examples/simple/my_plugin.js new file mode 100644 index 0000000..956683a --- /dev/null +++ b/examples/simple/my_plugin.js @@ -0,0 +1,45 @@ +var my_plugin = function(options){ + this.settings = options || {}; + + this.on("setup", this.set_time); + this.on("setup", this.add_headers); + + this.on("not-found", this.not_found); + + this.GET("/test", this.handle_test); + + this.GET("/", this.no_respond); + this.GET("/", this.handle_root); +}; + +my_plugin.prototype.set_time = function(request, server, done){ + request.current_time = new Date(); + done(); +}; + +my_plugin.prototype.add_headers = function(request, server, done){ + var time = request.current_time; + request.extra_headers = { + request_time: time.getHours() + ":" + time.getMinutes() + ":" + time.getSeconds(), + }; + done(); +}; + +my_plugin.prototype.handle_test = function(request, server, respond){ + respond(200, "thanks for visiting /test", request.extra_headers); +}; + +my_plugin.prototype.handle_root = function(request, server, respond){ + respond(200, "check out /test", request.extra_headers); +}; + +my_plugin.prototype.no_respond = function(request, server, respond){ + request.extra_headers.no_respond = "true"; + respond(); +}; + +my_plugin.prototype.not_found = function(request, server, respond){ + respond(404, "couldn't find handler for url: " + request.url, request.extra_headers); +}; + +module.exports = my_plugin;