如何在Firebase函数中使用koa.js + next

时间:2019-06-02 06:56:05

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

我想使用Cloud Functions for Firebase为shopify应用程序部署React应用程序。

我是Next和Koa的新手。

基于此repo,以下代码是如何在Firebase中托管简单的React应用程序。

const path = require('path')
const functions = require('firebase-functions')
const next = require('next')

var dev = process.env.NODE_ENV !== 'production'
var app = next({
  dev,
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
})
var handle = app.getRequestHandler()

exports.next = functions.https.onRequest((req, res) => {
  console.log('File: ' + req.originalUrl) // log the page.js file that is being requested
  return app.prepare().then(() => handle(req, res))
})

可以正常工作,没问题。

然后基于this tutorial from shopify,我需要在koa中集成server.js和其他依赖项,就我而言,我认为应该将其放置在firebase函数中。所以我得到了这段代码

const path = require('path')
const isomorphicFetch = require('isomorphic-fetch');
const Koa = require('koa');
const functions = require('firebase-functions')
const next = require('next');
const ShopifyConfig = require('./shopify.js');

const { default: createShopifyAuth } = require('@shopify/koa-shopify-auth');
const dotenv = require('dotenv');
const { verifyRequest } = require('@shopify/koa-shopify-auth');
const session = require('koa-session');

dotenv.config();

const port = parseInt(process.env.PORT, 10) || 3000;

var dev = process.env.NODE_ENV !== 'production'
var app = next({
  dev,
  conf: { distDir: `${path.relative(process.cwd(), __dirname)}/next` }
})
var handle = app.getRequestHandler()

const server = new Koa();

server.use(session(server));
server.keys = [ShopifyConfig.secretKey];

server.use(
  createShopifyAuth({
    apiKey: ShopifyConfig.key,
    secret: ShopifyConfig.secretKey,
    scopes: [],
    afterAuth(ctx) {
      const { shop, accessToken } = ctx.session;
      ctx.redirect('/');
    },
  }),
);

server.use(verifyRequest());

server.use(async (ctx) => {
  await handle(ctx.req, ctx.res);
  ctx.respond = false;
  ctx.res.statusCode = 200;

});

exports.next = functions.https.onRequest((req, res) => {
  console.log('File: ' + req.originalUrl) // 

  // This is old code
  // return app.prepare().then(() => {
  //   handle(req, res);
  // })

  // I tried this #1
  // server.callback(req, res);
})

// I tried this #2
// exports.next = functions.https.onRequest(server.callback);

// I tried this #3
// exports.next = functions.https.onRequest(server.callback());

// I tried this #4
exports.next = functions.https.onRequest((req, res) => {
  console.log('File: ' + req.originalUrl) 

  return app.prepare().then(() => {
    server.callback(req, res);
    //handle(req, res);
  })
})

我的问题现在基于Koa,functions.https.onRequest中应包含什么代码?请注意,没有代码可监听端口,因为它对Firebase函数没有意义。

我尝试过#1,#2,#3和this post

1->我收到请求超时

2->我收到请求超时

3->我收到“无法访问未定义的中间件”

4->我收到请求超时

2 个答案:

答案 0 :(得分:0)

感谢kvindasAB

server.callback本身不是回调,而是从我假设的配置中生成回调的函数。

因此代码需要更改为

server.callback()(req, res);

答案 1 :(得分:0)

const functions = require('firebase-functions');
const app = new Koa();

...

exports['http'] = functions.https.onRequest(app.callback());