1

This is a stand-alone broker application. I'm new to Express and Node, so I would like some advice on how to architect the structure of this application.

Starting off:

  • I have just an app.js file that is the server and opens the web socket.
  • Another (router.js) that has the Router(), and a list of REST calls.

However, this application is going to have a lot of REST calls, and is expecting a lot of data coming in and requests going out. So just having a list of get, post, etc. calls in router.js can get disorganized very quickly. Is there a better way to design this application?

1 Answer 1

2

create separate files for "contained" routes. For instance:

// stored as ./routes/abc.js
var middleware = require("../lib/middleware");
module.exports = {
  setup: function(app) {
    app.get("/abc/def", middleware.fn1, middleware.fn2, ..., this.def);
    app.get("/abc/[...]", this.[...]);
  },
  def: function(req, res) {
  },
  ...
}

and then in your ./routes/index.js something like:

var abc = require("abc");
module.exports = function(app) {
  ...
  abc.setup(app);
  ...
};

and then finally of course in your app.js, you get:

var express = require("express"),
    app = express();
require("routes")(app);
var port = process.env.PORT || 12345;
app.listen(port, function() {
  console.log('Listening on port %d', port);
});
Sign up to request clarification or add additional context in comments.

1 Comment

I like the technique but would recommend not naming the method 'bind' as maintainers may confuse it with the prototype 'bind' method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.