简体   繁体   English

使用Hapi.js框架的多个服务器实例

[英]Multiple server instances by using Hapi.js framework

I'm approaching to this framework for node.js for several reasons. 由于几个原因,我正在接近node.js的这个框架。 Simplicity, great modularity and a fast configuration out of the box. 简单,出色的模块化和开箱即用的快速配置。 I have soon encountered the concept of Pack which I have never seen during my experience in the learning of express.js framework. 我很快就遇到了Pack的概念,这是我在学习express.js框架的过程中从未见过的。 From the official guide the following example: 从官方指南中可以看出以下示例:

var Good = require('good');

server.pack.register(Good, function (err) {
    if (err) {
        throw err; // something bad happened loading the plugin
    }

    server.start(function () {
        server.log('info', 'Server running at: ' + server.info.uri);
    });
});

They says about Pack: 他们说包装:

Packs are a way for hapi to combine multiple servers into a single unit, and are designed to give a unified interface when working with plugins. 包是hapi将多个服务器组合到一个单元中的一种方式,用于在使用插件时提供统一的界面。

This concept is weird for me. 这个概念对我来说很奇怪。 How many times do we work with different servers in a project? 我们在项目中使用不同服务器的次数是多少次? In addition is not clear for me whether I should call pack every time to register a plugins in hapi. 另外我不清楚我是否应该每次调用打包在hapi中插件。

Update: This is pre v8 api code, the way to register a plugin has been changed. 更新:这是v8之前的api代码,注册插件的方式已经改变。 (call register directly on the server ) (直接在server上调用register

This concept is weird for me. 这个概念对我来说很奇怪。 How many times do we work with different servers in a project? 我们在项目中使用不同服务器的次数是多少次?

One example is when you have an api and a web server. 一个例子是你有一个api和一个web服务器。 These are usually developed separately, often in separate repositories. 这些通常是单独开发的,通常在单独的存储库中。 You could then create a third project which combines these plugins: 然后,您可以创建一个组合这些插件的第三个项目:

var Hapi = require('hapi');

var manifest = {
  servers: [
    {
      host: 'localhost',
      port: 8000,
      options: {
        labels: 'api',
        cors: true
      }
    },
    {
      host: 'localhost',
      port: 8001,
      options: {
        labels: 'web'
      }
    }
  ],
  plugins: {
    './example-api': [{select: 'api'}],
    './example-web': [{select: 'web'}]
  }
};

Hapi.Pack.compose(manifest, function(err, pack) {
  pack.start();
});

In addition is not clear for me whether I should call ever time pack to register a plugins in hapi. 另外我不清楚我是否应该随时打电话给hapi注册一个插件。

Yes, you need to call pack.register() when you want to register a plugin. 是的,当您想注册插件时,需要调用pack.register() However you can register more plugins at once: 但是,您可以一次注册更多插件:

plugin.register([
  require('crumb'),
  require('hapi-auth-cookie')
], function (err) {
  // Error handling
}

Visit Link: http://cronj.com/blog/hapi-mongoose 访问链接: http//cronj.com/blog/hapi-mongoose

Sample Project which can help you Repo Link: https://github.com/gauravgupta90/Hapi-Mongoose-Angular 示例项目可以帮助您回购链接: https//github.com/gauravgupta90/Hapi-Mongoose-Angular

For hapi version earlier than 8.x 对于早于8.x的hapi版本

var server = Hapi.createServer(host, port, {
    cors: true
});

server.start(function() {
    console.log('Server started ', server.info.uri);
});

For hapi new version 适用于hapi新版本

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ port: app.config.server.port });

Hapi.Pack() is no longer available in the newest versions of Hapi (8.x). 最新版本的Hapi(8.x)中不再提供Hapi.Pack() )。 They have pulled the functionality out into a small library called Glue. 他们已将功能扩展到一个名为Glue的小型库中。 You can find it here: https://github.com/hapijs/glue . 你可以在这里找到它: https//github.com/hapijs/glue It works exactly how Hapi.Pack() used to. 它完全适用于Hapi.Pack()以前的方式。

By default, hapi supports multiple connections and you can customize them individually, like installing plugins only for a selected server (like API only) . 默认情况下,hapi支持多个连接,您可以单独自定义它们,例如仅为选定的服务器(仅限API)安装插件

You can instantiate and kick off multiple connection like this: 您可以实例化并启动多个连接,如下所示:

const hapi = require('hapi');  
const port = 3000;  
const _ = require('lodash');

// Create hapi server instance
const server = new hapi.Server();

// add connection parameters
server.connection({  
    host: 'localhost',
    port: process.env.PORT || port
});

server.connection({  
    host: 'localhost',
    port: process.env.PORT + 1 || port + 1
});

// Start the server
server.start(function () {  
    // Log to the console the host and port info
    _.forEach(server.connections, function(connection) {
        console.log('Server started at: ' + connection.info.uri);
    });
});

Hope that helps. 希望有所帮助。

If you need more details about that topic, you can find them within this tutorial on how to run separate frontend and backend servers within a single project 如果您需要有关该主题的更多详细信息,可以在本教程中找到有关如何在单个项目中运行单独的前端和后端服务器的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM