Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
route-builder / lib / route-builder.coffee
Size: Mime:
_ = require 'lodash'
fs = require 'fs'
path = require 'path'
express = require 'express'
skip_if = require 'express-unless'
debug = require('debug') 'touchto:route-builder'

rest_cache = (req, res, next) ->
    res.header 'Cache-Control', 'no-cache, no-store, must-revalidate'
    res.header 'Pragma', 'no-cache'
    res.header 'Expires', 0
    next()
    return

rest_check = (req) ->
    if req.headers['accept'] and
    req.headers['accept'].match('json') isnt null
        return false
    if req.xhr then return false
    if req.headers['accept'] and
    req.headers['accept'].match(/text\/plain|text\/html|html/) isnt null
        return true
    return true

default_options =
    rest: false

buildrouter = ->
    options = {}

    if typeof arguments[arguments.length-1] is 'string'
        routedir = Array::pop.call arguments

    Array::forEach.call arguments, (argv) ->
        # If the argv is a function we should assume
        # that it is a route/middleware and insert it in
        # order before the matching routes
        if typeof argv is 'function'
            pre_routes.push argv

        # If the argv is an object we should assume
        # that it is a setup/options group and we need
        # to merge it with our defaults
        if typeof argv is 'object'
            options = _.extend default_options, argv
        return

    if options.rest
        router_app = express()
        router_app.use rest_cache
        router_app.disable 'etag'
    else
        router_app = express.Router
            caseSensitive: true

    routedir = path.join routedir, '.'
    fs.readdirSync routedir
        .forEach (file) ->
            if file.substr(0, 1) isnt '.'
                base = path.basename file, path.extname file
                base = base.match(/^(?:\d{0,}-)?(.+)/)[1]
                if base is 'index' then base = ''
                router_app.use "/#{encodeURI(base)}",
                    require path.resolve path.join routedir, file
                debug 'Registered route: %s', base
            return
    if options.rest
        router_app.skip_if = skip_if
        return router_app.skip_if rest_check
    else return router_app
module.exports = buildrouter