Giter Site home page Giter Site logo

bogart's Introduction

Getting Started

  • Download Narwhal.
  • Add the Narwhal bin directory to your PATH export PATH=$PATH:/path/to/narwhal/bin
  • Install Bogart tusk install bogart

Hello World in Bogart

var bogart = require("bogart");

exports.app = new bogart.App(function() {
  this.GET("/:name", function() {
    return this.text("hello " + this.params["name"]);
  });
});

Start your app: jackup -r app.js

Visit it in a web browser at http://localhost:8080.

Bogart with Jack.URLMap

var Bogart = require("bogart").App;
var URLMap = require("jack").URLMap;

var hello = new Bogart(function() {
  this.GET("/:name", function() {
    return this.text("hello " + this.params["name"]);
  });
});

var calc = new Bogart(function() {
  this.GET("/add/:first/:second", function() {
    var first = parseInt(this.params["first"]);
    var second = parseInt(this.params["second"]);
    return this.text("addition result: " + (first + second).toString());
  });
});

exports.app = URLMap({
  "/hello": hello,
  "/calc": calc
});

Events

All event callbacks receive the instance of App that called them as the first argument.

Bogart raises the following events:

  • "before_init" args: (app, options): Before any of the options passed to the App constructor have been evaluated.

  • "before_execute_route" args: (app, routeContext): Raised immediately before a route handler is executed. The callback function will receive two arguments. The first argument will contain a reference to the Bogart App raising the event. The second will contain the RouteHandlerContext that the route handler that is about to be executed will run inside of. Use this event to attach custom helpers to the route's context just before it is executed.

  • "after_execute_route" args: (routeContext): Raised after a route handler is executed. The callback function receives two arguments. The first argument contains a reference to the Bogart App raising the event. The second contains the RouteHandlerContext in which the route handler was executed. Use this event to clean up after your before_execute_route callback or to gather information from the RouteHandlerContext after the route has been executed.

  • "route_error" args: (app, error, routeInfo): Raised when a route handler throws an error. Callback function receives three arguments. The first argument contains a reference to te Bogart App raising the event. The second argument contains the error thrown by the route handler. The third arguments is a hash with the verb and path of the route that threw the error.

  • "before_render" args: (routeContext, templateContext): Raised right before rendering an EJS template. Useful for providing sane defaults to template variables that are only explicitly bound some of the time.

Subscribing to Events

var App = require("bogart").App;
App.subscribeTo("before_init", function(app, options) {
    options.cache_views = false;
});

EJS Templates

EJS Template helpers are available in bogart in the route handlers. Templates are assumed to be under 'views' relative to the root directory of the application.

EJS Layout (/views/site.ejs.html)

<html>
    <head><title>Hello</title></head>
    <body>
        <%= hold() %>
    </body>
</html>

EJS Template (/views/hello.ejs.html

<h1>Hello <%= name %></h1>

App

var Bogart = require("bogart").App;

exports.app = new Bogart(function() {
  this.layout = "site";

  this.GET("/:name", function() {
    return this.ejs("hello", { name: this.params.name });
  });
});

Results of navigating to /bob

<html>
    <head><title>Hello</title></head>
    <body>
        <h1>Hello bob</h1>
    </body>
</html>

Inspirations

Contributors

License

Copyright (c) 2009 Nathan Stott <nathan.whiteboard-it.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

bogart's People

Contributors

fitzgen avatar rhino-security avatar

Stargazers

 avatar

Watchers

 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.