node.js - How to warmup a serverless lambda with child routes in nodejs
问题描述
I have created a lambda function in nodejs with child route
Here my structure:
- lib
- routes
- notes
- note.js
- notes.controller.js
- otherRoutes
- index.js
- notes
- app.js
- db.js
- routes
- serverless.yml
Here 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;
I want to warmup my lambda function and I followed this tutorial :
https://serverless.com/plugins/serverless-plugin-warmup/
But the structure is different because it is multiple lambda function. In my structure, it is one lambda function with multiple child routes.
Do you know how can I warmup my lambda function with child routes please ?
Thank you for your help.
解决方案
自本周 re:Invent 以来,不再需要为 Lambda 函数设置复杂的升温策略,因为一个名为 Provisioned Concurrency 的新功能已启动,并由无服务器框架提供支持。只需在 serverless.yml 文件中添加一行,在您希望为其设置预置并发的函数下添加您想要多少个暖 Lambda 实例,然后一切就绪:
provisionedConcurrency: 3
无服务器框架的参考文档:https ://serverless.com/framework/docs/providers/aws/guide/functions/
包含更多详细信息的博客文章:https ://serverless.com/blog/aws-lambda-provisioned-concurrency/
推荐阅读
- css - 如何更改 C# MVC 项目的字体系列?
- javascript - 在 JavaScript 中同步获取请求数据
- magento - Magento 转移到另一台服务器
- azure-active-directory - “https://graph.microsoft.com/v1.0/users”端点返回用户,邮件字段为空
- arrays - PySpark - RDD 到 JSON
- javascript - 类型“只读<{}>”上不存在属性“xxx”
- wxpython - wxpython 获取底层GUI库
- php - 在 Laravel/Lumen Eloquent 中对折叠的集合进行排序
- concourse - Concourse To Slack 通知 - 不接收 text_file
- java - 无法在正确位置获得可点击的搜索视图项目