Skip to content

Instantly share code, notes, and snippets.

@dotproto
Forked from kelsin/app.js
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dotproto/16a15208871f2a54b528 to your computer and use it in GitHub Desktop.
Save dotproto/16a15208871f2a54b528 to your computer and use it in GitHub Desktop.
var express = require('express');
var app = express();
// Load routes
require('./src/routes')(app);
var client = require('../db.js');
modules.exports.index = function(req,res) {
res.render('issues/index', {issues: client.issues});
}
module.exports = function(){
var app = null;
function route(method, path, ctrlMethod) {
if (typeof ctrlMethod === "function") {
method(path, ctrlMethod);
}
}
function get(path, ctrlMethod) {
route(app.get, path, ctrlMethod);
}
function post(path, ctrlMethod){
route(app.post, path, ctrlMethod);
}
function del(path, ctrlMethod){
route(app.del, path, ctrlMethod);
}
function put(path, ctrlMethod){
route(app.path, path, ctrlMethod);
}
// MAIN
function self(path, controller) {
get(path, controller.index);
get(path + '/new', controller.new)
post(path, controller.create);
get(path + '/:id', controller.show);
get(path + '/:id/edit', controller.edit);
put(path + '/:id', controller.update);
del(path + '/:id', controller.delete);
}
self.init = function(appRef) {
app = appRef;
}
return self;
};
var issues = require('./controllers/issues');
var resource = require('./lib/resource');
module.exports = function(app){
resource.init(app);
resource('/issues', issues);
}
@anthonynichols
Copy link

Hey, I wrote something similar in Coffeescript for my media manager thing. Also, Express 4.x uses a different syntax for fancy routing, although I don't think the way in your example is broken or deprecated.

in routes.coffee

# ==============================================================================
# ROUTES
# ------------------------------------------------------------------------------

fs   = require "fs"
path = require "path"
ctrl = require("./controllers")()

routes = (app) ->

  app.resource = (path, controller = false, param = "id") ->
    controller = ctrl[path] if !controller
    app.route "/#{path}"
      .get (req, res) -> controller["index"] req, res
      .post (req, res) -> controller["create"] req, res
    app.route "/#{path}/new"
      .get (req, res) -> controller["new"] req, res
    app.route "/#{path}/:#{param}"
      .get (req, res) -> controller["show"] req, res
      .put (req, res) -> controller["update"] req, res
      .delete (req, res) -> controller["delete"] req, res
    app.route "/#{path}/:#{param}/edit"
      .get (req, res) -> controller["edit"] req, res

  app.resource "/foo"

module.exports = routes

in server.coffee

# ==============================================================================
# BASE SETUP
# ------------------------------------------------------------------------------

express    = require "express"
path       = require "path"
fs         = require "fs"
bodyParser = require "body-parser"

# ==============================================================================
# MOUNT MIDDLEWARE
# ------------------------------------------------------------------------------

app.use bodyParser()

# ==============================================================================
# STATIC ASSETS SETUP
# ------------------------------------------------------------------------------

app.use express.static path.join __dirname, "public"


# VIEW SETUP
app.set "views", path.join __dirname, "app/views"
app.set "view engine", "jade"
app.set "view options", { layout: true }

# ==============================================================================
# ROUTES
# ------------------------------------------------------------------------------

routes = require "./config/routes"
routes app

# ==============================================================================
# START THE SERVER
# ------------------------------------------------------------------------------

port = process.env.PORT || 3000
app.listen port

module.exports = app

in controllers/foo.coffee

module.exports.index = (req, res) ->
  console.log "From inside foo#index"
  res.send "foo#index"

module.exports.create = (req, res) ->
  console.log "From inside foo#create"
  res.send "foo#create"

module.exports.new = (req, res) ->
  console.log "From inside foo#new"
  res.send "foo#new"

module.exports.show = (req, res) ->
  console.log "From inside foo#show"
  res.send "foo#show"

module.exports.update = (req, res) ->
  console.log "From inside foo#update"
  res.send "foo#update"

module.exports.edit = (req, res) ->
  console.log "From inside foo#edit"
  res.send "foo#edit"

module.exports.delete = (req, res) ->
  console.log "From inside foo#delete"
  res.send "foo#delete"

And here's the code to auto-wire-up all files in controllers/*

# ==============================================================================
# CONTROLLER CONFIGURATION
# ------------------------------------------------------------------------------

fs   = require "fs"
path = require "path"

module.exports = ->
  controllers = {}

  __rootdirname = path.dirname(require.main.filename)
  controllersPath = path.join __rootdirname, "app/controllers"

  files = fs.readdirSync controllersPath

  for file in files
    stats = fs.statSync path.join(controllersPath, file)
    if stats.isFile()
      name = path.basename(file, path.extname(file))
      controllers[name] = require "#{controllersPath}/#{name}"

  return controllers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment