问题
I wanted to know if using Express.js as middleware in a Serverless app (AWS Lambdas) a good idea? My concern comes from the fact that in Express.js there is a mono-function setup and in future if lots of request come in it'll start throttling. Are my concerns valid or I am wavering about nothing.
回答1:
The decision between building a mono-lambda and one-function-per-endpoint does not have a crystal-clear answer.
On one hand, if you're using cloud formation - you're limited to 200 resources per stack, which can make even small APIs with single-lambda-per-route design cumbersome and force you down the path of things like stack splitting, which works but adds complication when there really shouldn't be one. Additionally, this makes it much harder to share code because each lambda function is its own deployment package - so you need to use things like a manually-included shared directory, local NPM modules, or good ol' fashioned copy-paste to share logic between your functions.
On the other hand, if you choose to route all requests to lambda and then use express inside your function to route requests - you're faced with managing some complexity that API Gateway can usually handle for you (things like request template validation, or route validation, or throttling/API usage management).
Ultimately you can build successful applications either way - so the important things for you to consider are:
- The number of API endpoints you're considering supporting
- The domain boundaries of these endpoints (are they very similar with lots of shared code? Or very unique and neatly bounded into their own services)
- The need for APIGW features such as throttling and request validation
- Comfort level of developers with or without a framework like express.
回答2:
I think: Express start a http server, but Serverless(azure function, lambda, ...) is an http server based on a simple script so... why you wanna start "another" http server?
Serverless is a great framework, so you don't need nothing more to work...
The same question applies with Loopbak, Fastify, Http/s, Hapi, ...
And about routes: https://aws.amazon.com/blogs/aws/api-gateway-update-new-features-simplify-api-development/
来源:https://stackoverflow.com/questions/64457577/should-i-be-using-express-js-in-a-serverless-app