Skip to content

Commit

Permalink
Merge pull request #1 from ericf/caridy-express-yui
Browse files Browse the repository at this point in the history
Tweaks to express-yui PR
  • Loading branch information
caridy committed Jul 19, 2013
2 parents ac29ea7 + 1746812 commit 6d07e2f
Show file tree
Hide file tree
Showing 9 changed files with 601 additions and 216 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
.env
build/
node_modules/
56 changes: 23 additions & 33 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var express = require('express'),
state = require('express-state'),
myui = require('express-yui'),
Locator = require('locator'),
var express = require('express'),
state = require('express-state'),
expyui = require('express-yui'),
Locator = require('locator'),
LocatorHandlebars = require('locator-handlebars'),

config = require('./conf/config'),
Expand All @@ -14,6 +14,7 @@ app = express();
app.set('name', config.name);
app.set('env', config.env);
app.set('port', config.port);
app.set('view', app.yui.view({defaultLayout: 'main'}));
app.set('state namespace', 'PNM');

app.enable('strict routing');
Expand All @@ -28,28 +29,23 @@ app.locals({

// -- Configure YUI ------------------------------------------------------------

// Global PNM env config.
global.PNM = {};
PNM.CACHE = config.cache.server;
PNM.FLICKR = config.flickr;

// custom modules should be registered manually or by adding a build.json
// to build them during boot, which is also supported in express-yui
// Custom modules should be registered manually, or by adding a `build.json` to
// build them during bootup. Both of which express-yui supports.
app.yui.applyConfig({
modules: {
"ios-oc-fix": "/vendor/ios-orientationchange-fix.js"
}
});

app.configure('development', function () {
app.yui.debugMode();
if (app.get('env') === 'development') {
app.yui.debugMode({filter: 'raw'});
app.yui.setCoreFromAppOrigin();
});

// -- Views ----------------------------------------------------------------

app.set('view', app.yui.view({
defaultLayout: 'main'
}));
}

// -- Middleware -----------------------------------------------------------

Expand All @@ -61,9 +57,10 @@ if (app.get('env') === 'development') {

app.use(express.compress());
app.use(express.favicon());
app.use(middleware.exposeYUI(expyui));
app.use(app.router);
app.use(middleware.slash());
app.use(myui.static());
app.use(expyui.static());
app.use(express.static(config.dirs.pub));
app.use(middleware.placeLookup('/places/'));

Expand Down Expand Up @@ -97,16 +94,14 @@ function exposeRoute(name) {
};
}

exposeRoute('index', '/', myui.expose(), routes.index);
exposeRoute('index', '/', routes.index);

exposeRoute('places', '/places/:id/', [
myui.expose(),
routes.places.load,
routes.places.render
]);

exposeRoute('photos', '/photos/:id/', [
myui.expose(),
routes.photos.load,
routes.photos.render
]);
Expand All @@ -116,24 +111,19 @@ app.expose(exposedRoutes, 'ROUTES');

// -- Locator and plugins ------------------------------------------------------

new Locator({
buildDirectory: 'build'
})
locator = new Locator({buildDirectory: 'build'})
.plug(LocatorHandlebars.yui())
.plug(app.yui.plugin({
registerGroup: true,
registerGroup : true,
registerServerModules: true
}))
.parseBundle(__dirname, {}).then(function () {

app.yui.use('pnm-helpers');

// the app is ready to receive traffic
}));

}, function (err) {
console.error(err);
console.error(err.stack);
});
locator.parseBundle(__dirname, {}).then(function () {
app.yui.use('pnm-helpers');
}, function (err) {
console.error(err);
console.error(err.stack);
});

// -- Exports ------------------------------------------------------------------

Expand Down
14 changes: 2 additions & 12 deletions conf/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,8 @@ var path = require('path'),
NODE_ENV = process.env.NODE_ENV,
PORT = process.env.PORT,

appRoot = process.cwd(),
config = require('./config.json');

// Poor-mans object clone.
function clone(config) {
return JSON.parse(JSON.stringify(config));
}

// Wrap crazy YUI API.
function mix(config, overrides) {
return Y.mix(config, overrides, true, null, 0, true);
}
appRoot = process.cwd(),
config = require('./config.json');

config.env = NODE_ENV || 'development';
config.port = PORT || config.port;
Expand Down
28 changes: 28 additions & 0 deletions middleware/expose-yui.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module.exports = function (expyui) {
return function (req, res, next) {
var method = req.method.toLowerCase(),
routes, match, noop;

if (method !== 'get') {
next();
return;
}

routes = req.app.routes[method];
match = routes && routes.some(function (r) {
return r.match(req.url);
});

if (!match) {
next();
return;
}

expyui.expose().forEach(function (middleware) {
var noop = function () {};
middleware(req, res, noop);
});

next();
};
};
1 change: 1 addition & 0 deletions middleware/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
exposeYUI : require('./expose-yui'),
placeLookup: require('./place-lookup'),
slash : require('express-slash')
};
Loading

0 comments on commit 6d07e2f

Please sign in to comment.