我在我的项目中使用了Firebase http函数,它们运行良好。当我尝试改善性能时,遇到了@DougStevenson先生在improve cold start performance by organising cloud functions in better way and using typescripts async importing of source file中发表的文章。
const app = express();
app.use(cors({origin: true}));
app.use(express.json());
app.post('/placeOrder', async (request: any, response: any) => {
//something
});
//export this constant in index.ts
export const order = functions
.region('asia-east2')
.https
.onRequest(app);
Index.ts
export const httpFn = functions.https.onRequest(async (request, response) => {
await (await import('./fn/httpFn')).myFun(request, response)
})
源文件
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
admin.initializeApp()
export const myFun = async ( request: functions.https.Request
, response: functions.Response): Promise<void> => {
const snapshot = await getDocument()
const data = snapshot.data()
response.send(data)
}
它们两者都运作良好。但是,当我尝试通过使用http post和Express.js使用相同的方法并异步调用调用加载源文件时,我无法做到这一点,而且在搜索中找不到相似的示例。
我尝试过
Index.ts
export const userOrder = functions.https.onRequest(async (request, response) => {
await (await import('./order')).default(request, response)
});
order.ts
const express = require('express');
const app = express();
app.use(express.json());
app.get('/getOrder', async (request: any, response: any) => {
response.status(200).send('It works!');
});
app.post('/createOrder', async (req: any, res: any) => {
//something
});
export default app;
但是这个还没有解决,我想知道如何使用Typescript的异步导入在云函数中部署Express js端点。
谢谢。