如何在Firebase云功能上使用hapi js?

时间:2017-06-03 01:30:56

标签: javascript node.js express firebase hapijs

我尝试向an example学习使用express与firebase上的把手一起使用。

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

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

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

See live functions

正如我所理解的那样,因为“express”就像http节点一样,所以它可以响应“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吗?

3 个答案:

答案 0 :(得分:2)

看看inject方法(最后一个代码示例):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)。

答案 1 :(得分:1)

这段代码很简单,因为我将Firebase使用的快速API与hapijs API混合在一起,这要归功于先生@dkolba先生给出的博客 您可以通过转到来定位url hapijs处理程序 http://localhost:5000/your-app-name/some-location/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");

});

答案 2 :(得分:0)

为了与hapijs 18.x.x兼容,需要进行一些更改

'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"));
});