Node.js, recovery and proper routing

I still hug my head around Node, but I have a very simple question. I see many node examples where people declare their routes and all their logic in one app.js file (or sometimes split them into subfiles).

My question basically is that it is better to keep all route declarations in the application or bootstrap as a common route that maps to your file structure. This may seem like a primitive question, but my goal is to understand what is most efficient in node.

I am currently creating an API handler with Restify, but I have another application that uses Express (so this question is likely to answer both questions).

In my route, I can either declare one bootstrap of the route like this:

app.all('/api/:package/:controller', function(request, response) { var controller = require( '../' + request.params.package + '/api/' + request.params.controller ); controller.index(request, response); response.end(); }); 

This basically takes all calls from the API and aims at the correct api controller. Alternatively, I can declare each route individually or perhaps even write a loop that goes through each of my controllers and declares them in init. So:

 for (var i in packages.controllers) { app.all('api/' + package + '/' + controllers[i].name, function(request, response) { var controller = require( '../' + request.params.package + '/api/' + request.params.controller ); controller.index(request, response); } } 

packages.controllers - an array of all possible controllers. Please note that the above code is not accurate, I have an HMVC folder structure, so the code is a little more complicated than the above. But you understand.

I wonder what the consequences of both are, and if it really matters?

Thanks!

+7
source share
3 answers

The explosive app.js file prompted us to create a small reference application to encode the standard structure of Express applications. This is not rocket science, but rather a set of conventions that make things more organized.

You can find it here: https://github.com/EAAppFoundry/tableau

We would like suggestions / pull requests if something went wrong or was missing.

+3
source

I would not recommend any app.js at all. You end up with a 5,000+ line file that will support the nightmare.

The biggest problem that I see in your snippet is that even if require () gets cached, it must execute a synchronous I / O request. It’s just a bad habit to join.

Like what Don recommends, I had the best luck splitting routes into modules that export a single function that takes an instance of the application. You can think of it as "decorating" an application instance:

 // app.js var app = express.createServer(); app.configure(function(){ //... }); require('./foo')(app); // foo.js exports = module.exports = function(app){ app.get('/whatever', function(req, res){}); }; 
+6
source

I don’t think there should be any real problem with the loop in the directory tree and the route generation. However, it will be difficult to define routable middleware and other routing functions (for example, variables in routes) with a good interface.

I wrote a library that I use to define my routes declaratively and with minimal repetition that may interest you. This inspired Rails for resourceful routing and quite flexible β€” the idea was to create a hash of routes and routines; There are also tools for defining route groups, middleware, and variables.

https://github.com/cheesun/express-declarative-routing

Although it does not automatically generate routes based on your directory structure, I think it will be a cool feature and welcome that you add it to the library.

0
source

All Articles