Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Init

  • Loading branch information...
commit 36907f0d3b74dd983d472ff45f4ba6d9db4597d4 0 parents
@1602 1602 authored
33 app.js
@@ -0,0 +1,33 @@
+/**
+ * Module dependencies.
+ */
+
+var express = require('express');
+
+var app = module.exports = express.createServer();
+
+// Configuration
+
+app.configure(function(){
+ app.set('views', __dirname + '/views');
+ app.set('view engine', 'jade');
+ app.use(express.bodyParser());
+ app.use(express.methodOverride());
+ app.use(app.router);
+ app.use(express.static(__dirname + '/public'));
+});
+
+app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+});
+
+app.configure('production', function(){
+ app.use(express.errorHandler());
+});
+
+// Routes
+
+require('./routes').init(app);
+
+app.listen(3000);
+console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
28 controllers/admin/users_controller.js
@@ -0,0 +1,28 @@
+exports.show = function (req, res) {
+ res.send('show');
+};
+
+exports.edit = function (req, res) {
+ res.send('edit');
+};
+
+exports.destroy = function (req, res) {
+ res.send('destroy');
+};
+
+exports.index = function (req, res) {
+ res.send('index');
+};
+
+exports.new = function (req, res) {
+ res.send('new');
+};
+
+exports.create = function (req, res) {
+ res.send('create');
+};
+
+exports.update = function (req, res) {
+ res.send('update');
+};
+
28 controllers/posts_controller.js
@@ -0,0 +1,28 @@
+exports.show = function (req, res) {
+ res.send('show');
+};
+
+exports.edit = function (req, res) {
+ res.send('edit');
+};
+
+exports.destroy = function (req, res) {
+ res.send('destroy');
+};
+
+exports.index = function (req, res) {
+ res.send('index');
+};
+
+exports.new = function (req, res) {
+ res.send('new');
+};
+
+exports.create = function (req, res) {
+ res.send('create');
+};
+
+exports.update = function (req, res) {
+ res.send('update');
+};
+
21 middleware.js
@@ -0,0 +1,21 @@
+exports.loadUser = function loadUser(req, res, next) {
+ console.log('Loading user');
+ req.user = {name: 'JB'};
+ process.nextTick(next);
+};
+
+exports.loadPost = function loadPost(req, res, next) {
+ console.log('Loading post');
+ req.post = {title: 'Hello world'};
+ process.nextTick(next);
+};
+
+exports.requireAdmin = function requireAdmin(req, res, next) {
+ if (req.param('admin')) {
+ console.log('Hello admin!');
+ next();
+ } else {
+ console.log('Hey! This area for privileged users only (pass ?admin=1 param to query string)');
+ res.send('admin required');
+ }
+};
10 package.json
@@ -0,0 +1,10 @@
+{
+ "name": "application-name"
+ , "version": "0.0.1"
+ , "private": true
+ , "dependencies": {
+ "express": "2.5.0"
+ , "jade": ">= 0.0.1"
+ , "railway-routes": ">= 0"
+ }
+}
51 routes.js
@@ -0,0 +1,51 @@
+var routing = require('railway-routes');
+var mw = require('./middleware');
+
+exports.init = function (app) {
+ var map = new routing.Map(app, handler);
+
+ // resourceful routes
+ map.resources('posts', {
+ middleware: mw.loadPost,
+ middlewareExcept: ['index', 'new', 'create']});
+
+ // admin namespace
+ map.namespace('admin', {middleware: mw.requireAdmin}, function (admin) {
+ admin.resources('users', {
+ middleware: mw.loadUser,
+ middlewareExcept: ['index', 'new', 'create']});
+ });
+
+ // generic routes
+ map.all('/:controller/:action');
+ map.all('/:controller/:id/:action');
+};
+
+// simple routes handler
+
+function handler(ns, controller, action) {
+ try {
+ var ctlFile = './controllers/' + ns + controller + '_controller';
+ var responseHandler = require(ctlFile)[action];
+ } catch(e) {}
+
+ return controller ? responseHandler || handlerNotFound : genericRouter;
+
+ function handlerNotFound(req, res) {
+ res.send('Handler not found for ' + ns + controller + '#' + action);
+ };
+
+ // to deal with :controller/:action routes
+ function genericRouter(req, res) {
+ var ctlFile = './controllers/' + ns + req.param('controller') + '_controller';
+ try {
+ var responseHandler = require(ctlFile)[req.param('action')];
+ } catch (e) {
+ responseHandler = function (req, res) {
+ res.send('Handler not found for ' + ns + req.param('controller') + '#' + req.param('action'));
+ }
+ }
+ responseHandler(req, res);
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.