title: Implement plugin-in architecture with JavaScript/Node.js
body: <p>Below is a simple node.js using express</p>
  
  <pre><code>var express = require('express');
  var app = express();
  
  app.get('/', function(req, res){
    res.send('Hello World');
  });
  
  app.listen(3000);
  </code></pre>
  
  <p>I want to implement a plugin-in architecture, such as by default there is a folder called <code>plugins</code>, and they register by themselve when the node.js startup, I don't need to modify the main <code>server.js</code> </p>
  
  <p>an example a <code>foo</code> plugin, e.g.</p>
  
  <pre><code>PluginManager.register("init, function(app) {
      app.get('/foo', function(req, res){
          res.send('Hello from foo plugin');
      });
  });
  </code></pre>
  
  <p>Then in my <code>server.js</code>, I would have something like</p>
  
  <pre><code>// TODO: Scan all scripts under the folder `plugins`
  // TODO: Execute all the `init` hook before the app.listen
  app.listen(3000);
  </code></pre>
  
  <p>My problem is my plugin codes are all asynchronous , there is no way to block before the <code>app.listen()</code>, do you have any idea for a better plugin-in architecture implementation?</p>
  
  <p>Thanks.</p>