Skip to content
Boris Filipov edited this page Aug 18, 2014 · 3 revisions

How it works

The organelle finds all files matched given pattern (usually using recursive search via /**/* pattern). Those files are then expected to export a builder function which results in object having "route": handler properties. Depending on the file location all routes from the object with their corresponding handlers are mounted to the expressjs app prefixed with the urlized file path.

index.js files are special ones and will be mounted first before any sibling or inner files without their name included.

For example this directory tree:

 - root
 |- a.js
 |- index.js
 |- folder
  |- _a.js
  |- index.js

Will be mounted in the following order:

  1. index.js
  2. a.js
  3. folder/index.js
  4. folder/_a.js

And handlers from those files will be available mapped at the following urls:

  • / -> index.js
  • /a -> a.js
  • /folder -> folder/index.js
  • /folder/_a -> folder/_a.js

examples

create single http GET action

// routes/action.js
module.exports = function(){
  return {
    "GET": function(req, res, next) {
      res.send("I'm GET action")
    }
  }
}

// open http://localhost:1337/action to see "I'm GET action" response

create nested actions (suitable for resources and collections)

// routes/resources/update.js
module.exports = function(){
  return {
    "POST": function(req, res, next) {
      res.send("I'm POST action of resources")
    },
    "DELETE": require("./my-implementation"),
    "PUT /inner/path": function(){}
  }
}

// POST to http://localhost:1337/resources/update to see response

// see "DELETE" and "PUT" methods has handlers too and their routes will be available respectively at
// DELETE http://localhost:1337/resources/update
// PUT http://localhost:1337/resources/update/inner/path

create catch all incoming requests action

// routes/index.js
module.exports = function(){
  return {
    "* *": function(req, res, next) {
      console.log("I'm first handler to be invoked per every incoming request")
      next()
    }
  }
}

// note that the index.js is placed in the root of the scanned directory tree
// so it is matching all routes 

create catch only inner actions action

// routes/resources/index.js
module.exports = function(){
  return {
    "* *": function(req, res, next) {
      console.log("I'm first handler to be invoked per every incoming request")
      next()
    }
  }
}

// note that the index.js is placed in the resources directory, 
// so it is matching only urls starting with /resources