繁体   English   中英

如何在 Firebase Cloud Function 上使用 hapi js?

[英]How to use hapi js on Firebase Cloud Function?

我尝试从一个例子中学习如何在 firebase 上使用 express 和把手。

对于快捷方式,我们可以将“app”实例直接发送到“functions.https.onRequest”,例如...

const app = express();
...
app.get('/', (req, res) => {
    ...
});

exports.app = functions.https.onRequest(app);

查看实时功能

据我了解,它可以正常工作,因为“express”就像 http-node,所以它可以响应“http plain”。

hapi相比,这里是 hello-world

const Hapi = require('hapi');
const server = new Hapi.Server();
server.connection({ 
    host: 'localhost', 
    port: 8000 
});

server.route({
    method: 'GET',
    path:'/hello', 
    handler: function (request, reply) {
        return reply('hello world');
    }
});

server.start((err) => {
    console.log('Server running at:', server.info.uri);
});

从 hapi 示例中,是否可以在 firebase 云功能上使用 hapi?

我可以在不启动像 express 这样的服务器的情况下使用 hapi 吗?

看看注入方法(最后一个代码示例): http : //www.carbonatethis.com/hosting-a-serverless-hapi-js-api-with-aws-lambda/

但是,我认为这是不可行的,因为您仍然需要保留Google Cloud Functions提供给 http 触发函数的快速应用实例的响应对象,因为只有send()redirect()end()将努力响应传入的请求,而不是 hapi 的方法(请参阅https://firebase.google.com/docs/functions/http-events )。

这段代码很简单,因为我将 Firebase 使用的 express API 与 hapijs API 混合在一起,这要归功于 @dkolba 先生提供的博客您可以通过访问http://localhost:5000/your-app-调用 url hapijs 处理程序-名称/某个位置/v1/hi

例如: http://localhost:5000/helloworld/us-central1/v1/hi

const Hapi = require('hapi');
const server = new Hapi.Server();
const functions = require('firebase-functions');

server.connection();

const options = {
    ops: {
        interval: 1000
    },
    reporters: {
        myConsoleReporter: [{
            module: 'good-squeeze',
            name: 'Squeeze',
            args: [{ log: '*', response: '*' }]
        }, {
            module: 'good-console'
        }, 'stdout']
     }
};

server.route({
    method: 'GET',
    path: '/hi',
    handler: function (request, reply) {
        reply({data:'helloworld'});
    }
});
server.register({
    register: require('good'),
    options,
}, (err) => {

    if (err) {
        return console.error(err);
    }

});



// Create and Deploy Your First Cloud Functions
// https://firebase.google.com/docs/functions/write-firebase-functions
exports.v1 = functions.https.onRequest((event, resp) => {
    const options = {
        method: event.httpMethod,
        url: event.path,
        payload: event.body,
        headers: event.headers,
        validate: false
    };
    console.log(options);
    server.inject(options, function (res) {
        const response = {
            statusCode: res.statusCode,
            body: res.result
        };
        resp.status(res.statusCode).send(res.result);
    });
    //resp.send("Hellworld");

});

需要进行一些更改才能与 hapijs 18.xx 兼容

'use strict';

const Hapi = require('@hapi/hapi')
, functions = require('firebase-functions');

const server = Hapi.server({
  routes: {cors: true},
});

server.register([ 
  {
    plugin: require('./src/plugins/tools'),
    routes: {
      prefix: '/tools'
    }
  }, 
]);

server.route({
  method: 'GET',
  path: '/',
  handler: (request, h) => {

    return 'It worlks!';
  }
});

exports.v1 = functions.https.onRequest((event, resp) => {
  //resp: express.Response

  const options = {
    method: event.httpMethod,
    headers: event.headers,
    url: event.path,
    payload: event.body
  };

  return server
    .inject(options)
    .then(response => {
      delete response.headers['content-encoding']
      delete response.headers['transfer-encoding']
      response.headers['x-powered-by'] = 'hapijs'
      resp.set(response.headers);
      return resp.status(response.statusCode).send(response.result);
    })
    .catch(error => resp.status(500).send(error.message || "unknown error"));
});

暂无
暂无

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

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