Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Prevent multiple adds of the same route

  • Loading branch information...
commit 2bbac48cb7d9595d6881b484c9be101d7cd13a57 1 parent 1b424f2
@aredridel authored
Showing with 12 additions and 7 deletions.
  1. +12 −7 lib/route-watcher.js
View
19 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() {
@@ -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))
+ }
}
}
@@ -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);
}
Please sign in to comment.
Something went wrong with that request. Please try again.