Hapi.js子域路由到插件

我正在寻找一种方法将不同的子域路由到不同的插件。 我浏览了API文档 ,没有发现任何有用的信息。

我最终创build了一个简单的类来创build只能在特定子域上工作的插件。 这里是。

 var Plugin = function(attributes, routes) { // Add our routes to the server this.register = function(plugin, options, next) { // Loop through the selected servers and add the routes plugin.servers.forEach(function(server) { // Loop through the routes and add the vhost option routes.map(function(route) { route.vhost = attributes.vhosts.map(function(vhost) { return vhost + "." + server.info.host; }); }); // Add the routes server.route(routes); }); next(); }; // Add our attributes this.register.attributes = attributes; }; 

然后,您可以创build一个新的插件,并轻松地指定子域名。 例:

 var plugin = new Plugin([ // Your route or routes here ], { vhosts: ["array", "of", "subdomains"] });