Skip to content

Instantly share code, notes, and snippets.

@kelsin
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kelsin/eaa41c672684ffccdacc to your computer and use it in GitHub Desktop.
Save kelsin/eaa41c672684ffccdacc to your computer and use it in GitHub Desktop.
Sample express app
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});
}
var issues = require('./controllers/issues');
module.exports = function(app){
app.get( '/issues', issues.index);
app.get( '/issues/new', issues.new);
app.post('/issues', issues.create);
app.get( '/issues/:id', issues.show);
app.get( '/issues/:id/edit', issues.edit);
app.put( '/issues/:id', issues.update);
app.del( '/issues/:id', issues.delete);
}
@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