Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Prevent multiple adds of the same route
  • Loading branch information
aredridel committed Jan 1, 2012
1 parent 1b424f2 commit 2bbac48
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions lib/route-watcher.js
@@ -1,9 +1,9 @@
var fs = require('fs');

exports.route = function route(path, require) {
function makeRoute(path, require) {
var realPath = require.resolve(path);
console.log('watching ' + realPath);
module = require(realPath);
var module = require(realPath);
function watch() {
fs.watch(realPath, {persistent: false}, function() {}).once('change', function() {
setTimeout(function() {
Expand All @@ -16,21 +16,26 @@ exports.route = function route(path, require) {
})
}
watch();
return function(req, res, next) {
var f = function(req, res, next) {
if(module) {
module.apply(null, arguments)
} else {
next()
}
}
f.watchedRoute = true;
return f;
}

exports.plumb = function plumb(app, routes, require) {
function plumb(app, routes, require) {
console.log("plumbing routes", routes);
for(var i in routes) {
var route = routes[i]
console.log('plumbing ' + route.method + ' of ' +route.path + ' to ' + route.module);
app[route.method](route.path, exports.route("./routes/" + route.module, require))
var key = route.regexp ? Regexp.new(route.regexp, route.flags) : route.path;
if (!app.routes.lookup(key).watchedRoute) {
app[route.method](key, makeRoute("./routes/" + route.module, require))
}
}
}

Expand All @@ -51,11 +56,11 @@ exports.load = function load(app, routesFile, require) {
console.log(err);
return;
}
exports.plumb(app, JSON.parse(data), require)
plumb(app, JSON.parse(data), require)
});
}

exports.loadSync = function loadSync(app, routesFile, require) {
exports.plumb(app, JSON.parse(fs.readFileSync(routesFile)), require);
plumb(app, JSON.parse(fs.readFileSync(routesFile)), require);
exports.watch(app, routesFile, require);
}

0 comments on commit 2bbac48

Please sign in to comment.