Google Cloud Functions, Node JS 8.9.x (LTS) and KOA library

前端 未结 4 1340
Happy的楠姐
Happy的楠姐 2021-01-13 02:18

How can I use Koa library, the express replacement, in Cloud Functions?

I know KOA use all great ES2017 and make more use of Async use of JavaScript.

or it

4条回答
  •  余生分开走
    2021-01-13 03:03

    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"
           .
           .
      }
    

提交回复
热议问题