如何在 nodejs 中使用子路由预热无服务器 lambda

How to warmup a serverless lambda with child routes in nodejs

我在带有子路由的 nodejs 中创建了一个 lambda 函数
这是我的结构:


这里index.js

const express = require('express');
const notes = require('./notes/notes.controller');
router.use('/notes', notes);
router.use('/otherRoutes', otherRoutes);
module.exports = router;

app.js

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

/*CORS*/
app.use((req, res, next) => {
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader(
        "Access-Control-Allow-Headers",
        "Origin, X-Requested-With, Content-Type, Accept"
    );
    res.setHeader(
        "Access-Control-Allow-Methods",
        "GET, PUT, POST, PATCH, DELETE, OPTIONS"
    );
    next();
});
const helmet = require('helmet');
app.use(helmet());
require('./db');
const routes = require('./routes');
app.use('/', routes);

module.exports = app;

notes.controller.js

const express = require('express');
const notesController = express.Router();
const Note = require('./note');

notesController
    .post('/', async (req, res, next) => {
        const note = await Note.create(req.body);
        res.status(200).send(note)
    });

notesController
    .put('/:id', async (req, res, next) => {
        const note = await Note.findByIdAndUpdate(req.params.id,
            { $set: req.body },
            { $upsert: true, new: true });
        res.status(200).send(note)
    });

notesController
    .get('/', async (req, res, next) => {
        const notes = await Note.find();
        res.status(200).send(notes)
    });

notesController
    .get('/:id', async (req, res, next) => {
        const note = await Note.findById(req.params.id);
        res.status(200).send(note)
    });

notesController
    .delete('/:id', async (req, res, next) => {
        const note = await Note.deleteOne({ _id: req.params.id });
        res.status(200).send(note)
    });

module.exports = notesController;

我想预热我的 lambda 函数,我遵循了本教程: https://serverless.com/plugins/serverless-plugin-warmup/
但结构不同,因为它是多个 lambda 函数。在我的结构中,它是一个具有多个子路由的 lambda 函数。
你知道我如何用子路由预热我的 lambda 函数吗?

感谢您的帮助。

自从本周 re:Invent 以来,无需为 Lambda 函数设置复杂的预热策略,因为新功能 calle Provisioned Concurrency 已启动并受 Serverless Framework 支持。只需在您的 serverless.yml 文件中添加一行到您想要预置并发集的函数下,以及您想要多少个热 Lambda 实例,您就全部设置好了:

    provisionedConcurrency: 3

无服务器框架的参考文档:https://serverless.com/framework/docs/providers/aws/guide/functions/

博客 post 更详细:https://serverless.com/blog/aws-lambda-provisioned-concurrency/