【问题标题】:"Mount" (run) legacy http handler in Hapi.jsHapi.js 中的“挂载”(运行)旧版 http 处理程序
【发布时间】:2014-07-20 10:07:27
【问题描述】:

我做了一个 Node.js 聚会演示,但无法回答这个问题。它仍然困扰着我。

假设我有一个旧的 http 应用程序或 Express.js 应用程序。它是一个函数,形式为

function legacy_app(request, response) {
  // Handle the request.
}

假设我将 Hapi.js 用于我的应用程序的新版本。但是我有很多经过调试的遗留代码或上游代码,我希望将它们集成到 Hapi 应用程序中。例如,旧版 vhost 将运行旧版,或者可在 URL 中的 /legacy 命名空间内访问。

最好的方法是什么?

【问题讨论】:

    标签: node.js http hapijs


    【解决方案1】:

    包装现有的 HTTP 节点服务器调度函数以用作 hapi 处理程序可能没问题,但您必须添加到 hapi_wrap 函数(最后):

    reply.close(false);

    这样 hapi 可以完成对请求的处理,而不会弄乱您的旧逻辑 (https://github.com/spumko/hapi/blob/master/docs/Reference.md#replycloseoptions)。

    包装 Express 处理程序/中间件要复杂得多,因为您可能依赖于其他一些中间件(例如正文解析器、cookie 解析、会话等)并使用一些不属于节点的 Express 装饰器(例如 res .send()、res.json() 等)。

    【讨论】:

    • 非常感谢!我认为如果无错误非常重要,我也会考虑使用 Hapi 的反向代理支持。是的,它使部署更加复杂(多个服务侦听)但是现在我想起来了,这基本上是代理功能的目的(或至少是它的目的之一)。
    【解决方案2】:

    我能想到的唯一方法是手动。直接破坏文档中的建议:提取原始请求和响应对象并将它们传递给旧版处理程序。

    // An application built with http core.
    var http = require('http')
    var legacy_server = http.createServer(legacy_handler)
    function legacy_handler(request, response) {
      response.end('I am a standard handler\n')
    }
    
    // An express application.
    var express = require('express')
    var express_app = express()
    express_app.get('*', function(request, response) {
      response.send('I am an Express app\n')
    })
    
    // A Hapi application.
    var Hapi = require('hapi')
    var server = new Hapi.Server(8080, "0.0.0.0")
    server.route({path:'/', method:'*', handler:hapi_handler})
    function hapi_handler(request, reply) {
      reply('I am a Hapi handler\n')
    }
    
    // Okay, great. Now suppose I want to hook the legacy application into the
    // newer Hapi application, for example under a vhost or a /deprecated namespace.
    server.route({path:'/legacy', method:'*', handler:hapi_wrap(legacy_handler)})
    server.route({path:'/express', method:'*', handler:hapi_wrap(express_app)})
    
    // Convert a legacy http handler into a Hapi handler.
    function hapi_wrap(handler) {
      return hapi_handler
      function hapi_handler(request, reply) {
        var req = request.raw.req
        var res = request.raw.res
    
        reply.close(false)
        handler(req, res)
      }
    }
    
    legacy_server.listen(8081)
    express_app.listen(8082)
    server.start()
    

    这似乎行得通,但如果熟悉 Hapi 的人能够确认它没有错误,我会很高兴。

    $ # Hit the Hapi application
    $ curl localhost:8080/
    I am a Hapi handler
    
    $ # Hit the http application
    $ curl localhost:8081/
    I am a standard handler
    
    $ # Hit the Express application
    $ curl localhost:8082/
    I am an Express app
    
    $ # Hit the http application hosted by Hapi
    $ curl localhost:8080/legacy
    I am a standard handler
    
    $ # Hit the Express application hosted by Hapi
    $ curl localhost:8080/express
    I am an Express app
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-08-28
      • 1970-01-01
      • 1970-01-01
      • 2019-02-22
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多