2017-06-03 34 views
0

我試圖從an example中學習使用express和firebase上的把手。如何在Firebase雲端函數上使用hapi js?

快車的方式,我們可以直接將「應用」實例的「functions.https.onRequest」像......

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

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

See live functions

正如我的理解它的工作,因爲「表達「行爲像http-node,所以它可以迴應」http plain「。

相較於高致病性禽流感,這裏你好世界

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例子,是有可能的火力點雲功能使用高致病性禽流感?

我可以在不啓動服務器的情況下使用hapi嗎?

+0

你應該使用hapi或快速消防基地。消防基地Handels路由給你,所以不需要路由框架。保持你的功能重量輕。如果你想渲染車把模板只需要把手。 – philipheinser

回答

0

此代碼是直線前進,因爲我是混合與hapijs API使用火力地堡明確API,這要歸功於老總@dkolba給出的博客 您可以將 http://localhost:5000/your-app-name/some-location/v1/hi

例如ivoke網址hapijs處理程序: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"); 

}); 
相關問題