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

Rez*_*eza 5 next node.js firebase koa google-cloud-functions

我想使用 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))
})
Run Code Online (Sandbox Code Playgroud)

哪个工作正常,没问题。

然后根据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);
  })
})
Run Code Online (Sandbox Code Playgroud)

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

我尝试了 #1、#2、#3 以及这篇文章

1 -> 我收到请求超时

2 -> 我收到请求超时

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

4 -> 我得到请求超时

Rez*_*eza 0

感谢kvindasAB

server.callback 本身并不是回调,而是一个根据我假设的配置生成回调的函数。

所以代码需要改为

server.callback()(req, res);
Run Code Online (Sandbox Code Playgroud)