diff --git a/lib/route-watcher.js b/lib/route-watcher.js index c5ba6d1..20cc883 100644 --- a/lib/route-watcher.js +++ b/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); }