我想将插件添加到具有多个连接的hapi服务器,例如侦听不同的ips。
是否可以将插件添加到所有已配置的服务器?
或者如何遍历所有服务器以将插件添加到所有服务器?
默认情况下,插件会在调用时为所有连接添加路由server.route()
。
要限制插件向其添加路由的连接,可以在创建连接时使用标签,然后在注册插件时指定这些标签。这是一个例子:
var Hapi = require('hapi');
var server = new Hapi.Server();
server.connection({ port: 8080, labels: 'a' });
server.connection({ port: 8081, labels: 'b' });
server.connection({ port: 8082, labels: 'c' });
var plugin1 = function (server, options, next) {
server.route({
method: 'GET',
path: '/plugin1',
handler: function (request, reply) {
reply('Hi from plugin 1');
}
});
next();
};
plugin1.attributes = { name: 'plugin1' };
var plugin2 = function (server, options, next) {
server.route({
method: 'GET',
path: '/plugin2',
handler: function (request, reply) {
reply('Hi from plugin 2');
}
});
next();
};
plugin2.attributes = { name: 'plugin2' };
server.register(plugin1, function (err) {
if (err) {
throw err;
}
server.register(plugin2, { select : ['a'] }, function (err) {
if (err) {
throw err;
}
server.start(function () {
console.log('Server started');
})
});
});
GET / plugin1路由来自plugin1
响应:
http://localhost:8080/plugin1
http://localhost:8081/plugin1
http://localhost:8081/plugin2
其中GET / plugin2路由来自plugin2
仅响应:
http://localhost:8080/plugin2
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句