serverless-aws-lambda
provied Router
may be used to write a Lambda with ExpressJs compatible syntax, which supports ALB and API Gateway events (including multiValueQueryStringParameters and multiValueHeaders)
To get Type definitions please set "moduleResolution": "NodeNext"
inside your tsconfig.json
.
// src/controllers/playersController.ts
import type { RouteController } from "serverless-aws-lambda/router";
export const playersController: RouteController = async (req, res, next) => {
// dummy app logic
const foundUser = await getUserById(req.query.id);
res.json(foundUser);
};
// src/routes/players.ts
import { Router } from "serverless-aws-lambda/router";
import { auth } from "../controllers/auth";
import { playersController } from "../controllers/playersController";
const route = Router();
route.use(auth, playersController);
route.use((error, req, res, next) => {
console.log(error);
res.status(500).send("Internal Server Error");
});
export default route;
route.use
is similar to Express app.use(...), a function (async or not) which accepts 3-4 arguments. request, response and next.
const route = Router();
route.use(auth);
route.use(playersController);
route.use((error, req, res, next) => {
console.log(error);
res.status(500).send("Internal Server Error");
});
or by chaning:
const route = Router();
const errorHandler = (error, req, res, next) => {
console.log(error);
res.status(500).send("Internal Server Error");
};
route.use(auth).use(playersController).use(errorHandler);
or with multi argument:
import { Router } from "serverless-aws-lambda/router";
const handler = Router();
const errorHandler = (error, req, res, next) => {
console.log(error);
res.status(500).send("Internal Server Error");
};
handler.use(auth, playersController, errorHandler);
export { handler };
property | type | doc | info |
---|---|---|---|
body | any | doc | Request with json content-type are automatically parsed. Use body-parser middleware from serverless-aws-lambda/body-parser to parse Form Data and files |
cookies | key-value | doc | compatible with Express's cookie-parser |
method | string | doc | |
params | string[] | As we don't handle custom routes we can't support named params, instead params will return an array of string containing path components separated by / (without query string) |
Not compatible with Express |
path | string | doc | |
protocol | string | doc | |
query | key-value | doc | |
get | function | doc |
++ includes also event
raw object from AWS Lambda (except "cookies
" which can be easly parsed with cookie-parser
middleware)
property | type | doc | info |
---|---|---|---|
locals | key-value | doc | |
cookie | function | doc | |
clearCookie | function | doc | |
end | function | Return anything from your lambda. All previous setters are ignored. | Not compatible with Express |
get | function | doc | |
json | function | doc | |
links | function | doc | |
location | function | doc | |
redirect | function | doc | |
send | function | doc | |
set | function | doc | |
status | function | doc | |
type | function | doc |
++ includes also context
object from AWS Lambda and the third AWS Lambda handler argument "callback
"
Similar to ExpressJs next function.
next()
can take one argument.
If an argument is provided Router triggers next middleware which has 4 arguments.
This is usally used to handle errors (see examples above).
Check Express documentation for more info.