Google Cloud Functions,Node JS 8.9.x(LTS)和KOA库

时间:2017-12-08 10:08:28

标签: node.js firebase google-cloud-functions koa

如何在云函数中使用Koa库,快速替换?

我知道KOA使用了所有优秀的ES2017并且更多地使用了异步使用JavaScript。

或者可能根本不需要使用云功能,因为Firebase系统在结束前一个云功能之前不会向同一个云功能发送多个呼叫?

我不清楚。

它知道需要Node 8.x并且我知道NodeJs 8.9.x,现在已经是LTS。

4 个答案:

答案 0 :(得分:4)

从云功能doc中读取:

  

Base Image Cloud Functions使用基于Debian的执行环境   并包含gcr.io/google-appengine/nodejs Docker的内容   在指定的版本中安装了Node.js运行时的image   以上:

FROM gcr.io/google-appengine/nodejs
RUN install_node v6.14.0
  

要查看图像中包含的内容,您可以检查其GitHub project,或者拉动并检查图像本身。更新语言   运行时(Node.js)通常是自动完成的(除非另有说明)   通知),并包括基数定义的任何变化   图像。

我在2017年11月看到了一个pull request,添加了Nodejs v8。这里希望它最终可以登陆谷歌云功能

更新:Google Cloud Functions现在支持Node.js 8 and even Python

答案 1 :(得分:3)

参阅Google的发布说明...... Cloud Functions Release Notes

支持的节点版本仍然是v6,对于firebase也是如此。你需要等待一段时间才能在v8中发布它。我很确定当v6不再受支持时他们会转向v8,但希望早些时候......

答案 2 :(得分:0)

Use babel:

index.js:
----------=

'use strict';

require('@babel/register')
require('babel-polyfill')
const http = require('http')

const createApp = require('./app/app.js')
const handle = createApp().callback()

if (process.env.IS_LOCAL_DEPLOYMENT) {
    // to use same code in local server
    http.createServer(handle).listen(3000)
} else {
    module.exports.http = (request, response) => {
        handle(request, response)
    };
}

app.js:
--------

'use strict';

const Koa = require('koa')

module.exports = () => {
    const app = new Koa()

    app.use(......)

    return app
}

package.json
------------

  "scripts": {
       .
       .
    "start": "export IS_LOCAL_DEPLOYMENT=true && node index"
       .
       .
  }

答案 3 :(得分:0)

我刚刚在Cloud Functions Console编辑器中看到了我的一项功能,即Node 8现在是运行时选项。查看屏幕截图:

enter image description here