lance

A sharp node web framework

Lance [[ PRE-RELEASE IN-DEVELOPMENT ]]

The alpha-strike web server.

  • Give lance a config
  • lance.initailize()
  • You're done

The parsing of the config is advanced, allowing for high flexibility with sane defaults.

The default config is effectively an API reference.

Lance = require 'lance'
 
lance = new Lance {
    routes: [
        [
            "GET"'/:api(a|b|c|d)'({ route }) ->
                console.log 'ayyy' if route.api is 'a'
                o.serveHttpCode 500
        ]
    ]
}
 
lance.initialize().then -> # Server is up 
###
The directory structure:
/project/
    /static/
    /views/
        index.jade
        style.styl
        app.coffee
    
    server.coffee (this)
###
Lance = require 'lance'
 
lance = new Lance {
    templater:
        findIn: './views'
        saveTo: './static'
        
        ###
        Bundle up all Stylus/Css or Coffee/Js dependencies into a single file
        by utilizing Stylus and browserify requires.
        ###
        bundle:
            # destination   : source 
            "style.css"     : "style.styl"
            "app.js"        : "app.coffee"
            
        templater:
            ext: '.jade'
            engine: require 'jade'
}
 
# Routes can be defined after instantiation, outside of the config, too 
 
lance.router.GET '/:api(a|b|c|d)'(o) ->
    ###
    `o` is a special options object containing request information in a pre-parsed manner. `o` is passed to all route requests in place of `req` and `res`.
    
    More info on this later.
    ###
    
    if o.route.api is 'a'
        console.log 'ayyy'
    
    # This will resolve to our ./project/views/index.jade file 
    o.template.view = 'index'
    
    ###
    o.serve() serves a response based on either arguements passed to it or depending on properties in `o`.
    
    if `o.template.view` is set, the template will be rendered
    if `o.redirect` is set, a redirection will occur
    if neither are set, Lance serves JSON by default.
    
    You may also instead call `o.serveTemplate()`, `o.serveHttpCode()`, `o.serveJson()`, `o.serveRedirect()` or a basic `o.respond()`
    ###
    o.serve()
    
###
This will build the templates then start the server, all depending on the config. 
###
lance.initialize().then ->
    # we're ready 
 
###
Alternitavely...
 
There is a choice between `lance.initialize()`, which initializes everything in order, and manually initializing aspects of lance for more control.
###
 
lance.templater.initialize().then ->
    # The bundles have been compiled to: 
    #   ./projectDirectory/static/style.css 
    #   ./projectDirectory/static/app.js 
    # from the ./projectDirectory/views directory. 
    # and will be watched for changes (and any of their dependencies) 
    # then recompiled automatically 
 
    lance.start().then ->
        # we're ready 
        
 

You can specify your own templater middleware. If none are specified, then Lance will default to checking whether ect is installed and will use that.

 
new Lance {
    templater:
        templater:
            ext: '.jade'
            engine: require 'jade'
            options: {} # Supplied to the engine on instantiation 
}

Lance handles these mostly automatically:

  • CSS and Stylus
  • CoffeeScript, CJSX and Javascript
  • Static assets such as images, json, robots.txt etc.

On initialization:

  • Bundles are rendered to the static directory
  • Bundles are watched for changes, then rerendered
  • Assets files are copied over to the static directory
  • Assets files are watched for changes, then resaved
  • Directories are watched for new directories and files

By default all static assets that match the regexp (found in the config's templater.assets.match) will be copied over to the static directory.

  • For some filetypes, such as images, this means they can also be optimized.
    • TODO: Impliment asset stream hooking
new Lance {
    templater:
        findIn: './views'
        saveTo: './static'
        
        ###
        true by default, this causes `assets` to keep their directory structure inside the saveTo folder.
        ###
        preserveDirectory: true
        
        bundle:
            # destination   : source 
            "style.css"     : "style.styl"
            "app.js"        : "app.coffee"
}

The result is that the static directory will always have only what you want to make public, in one place, with a directory structure that will mirror your views.

See the The default config for how to build the configs for maximum flexibility.

Lance has become a proper node_module

  • Source compiled to .js from ./src/, as it should be.
  • Modular requires
    • require 'lance Function Lance
    • require 'lance/utils All utilities
    • require 'lance/utils/core Minimal, browser-friendly utilities only
    • require 'lance/utils/transpose Only the transposer
  • Merge utils with helpers
  • Compile from ./src/ to publish javascript instead of coffeescript
  • Document the config file
  • Figure out hooks
  • Renamed prettyErrorStack to error
  • helpers folder nuked
  • API
    • lance.events replaced with lance.eventHandler
    • lance.relayEvents and lance.extendEmitter in favor of EventHandler
      • lance.eventHandler.relay is lance.relayEvents
      • lance.eventHandler.extendEmitter is lance.extendEmitter
    • Removed utils.isIterable
    • Utils
      • .typeOf

        • number check changed
        • removed lowercase properties
      • Added .checkFor

      • Added .format

      • Removed: .isNumber, .sort, .isAbsolute .isExt, .isUrl, .camelCase .slugify, .changeExt, .minify .minifyCss, clone.merge.hard

      • clone.merge is now functionally clone.merge.hard