这两种在无服务器中处理API路由的方法之间在功能或性能上是否存在差异。就内存和容器缓存的使用方式而言,这些是否相同?
serverless.yml
functions:
api:
handler: lambda.handler
events:
- http: ANY /foo
- http: ANY /home
handler.js
exports.router = async (event, context) => {
const { path } = event
switch (path) {
case '/foo':
return require('./handlers/foo')(event, context)
case '/home':
return require('./handlers/home')(event, context)
}
}
handlers / foo.js
module.exports = async (event, context) => {
...
};
handlers / home.js
module.exports = async (event, context) => {
...
};
serverless.yml
functions:
apifoo:
handler: handler.foo
events:
- http: ANY /foo
apihome:
handler: handler.home
events:
- http: ANY /home
handler.js
export const foo = async (event, context) => {
...
};
export const home = async (event, context) => {
...
};
也许这些将处理相同的内容?我现在正在使用方法1,但想确保自己表现得更好。我知道aws会在每次请求时重新加载lambda,但会保持代码在lambda本身之外运行,如果这两者中有更好的设置,则会感到有点好奇。