I would like to develop sample apps and its routing by using serverless-framework
. I send following command
sls create -t aws-nodejs-typescript -n sample-api-dev
It generate following directory
.
├── src
│ ├── functions # Lambda configuration and source code folder
│ │ ├── hello
│ │ │ ├── handler.ts # `Hello` lambda source code
│ │ │ ├── index.ts # `Hello` lambda Serverless configuration
│ │ │ ├── mock.json # `Hello` lambda input parameter, if any, for local invocation
│ │ │ └── schema.ts # `Hello` lambda input event JSON-Schema
│ │ │
│ │ └── index.ts # Import/export of all lambda configurations
│ │
│ └── libs # Lambda shared code
│ └── apiGateway.ts # API Gateway specific helpers
│ └── handlerResolver.ts # Sharable library for resolving lambda handlers
│ └── lambda.ts # Lambda middleware
│
├── package.json
├── serverless.ts # Serverless service file
├── tsconfig.json # Typescript compiler configuration
├── tsconfig.paths.json # Typescript paths
└── webpack.config.js # Webpack configuration
handler.ts
import type { ValidatedEventAPIGatewayProxyEvent } from '@libs/api-gateway';
import { formatJSONResponse } from '@libs/api-gateway';
import { middyfy } from '@libs/lambda';
import schema from './schema';
const hello: ValidatedEventAPIGatewayProxyEvent<typeof schema> = async (event) => {
return formatJSONResponse({
message: `Hello ${event.body.name}, welcome to the exciting Serverless world!`,
event,
});
};
export const main = middyfy(hello);
after that I send following command
sls local start
MAC0157:$ sls offline start
Running "serverless" from node_modules
Starting Offline at stage dev (us-east-1)
Offline [http for lambda] listening on http://localhost:3002
Function names exposed for local invocation by aws-sdk:
* hello: pricing-api-dev-dev-hello
┌─────────────────────────────────────────────────────────────────────────┐
│ │
│ POST | http://localhost:3000/dev/hello │
│ POST | http://localhost:3000/2015-03-31/functions/hello/invocations │
│ │
└─────────────────────────────────────────────────────────────────────────┘
Server ready: http://localhost:3000 🚀
My question is
① How can I set routing in each function? for example, I would like to add GET
function in my project.I couldn't find how to set routing in my function.Where is POST
defined in hello/handler.ts
?
② How can I change environment? it seems that offline start at stage dev ( us-east-1), how can I change this stage? Are there any setting needed?
If someone has opinion or materials will you please let me know. Thanks
Thanks
You can still use the same function signature for get request.
Just write it this way for get request:
const getFunc: ValidatedEventAPIGatewayProxyEvent<typeof undefined> = async (event) => {
//Your function logic goes in here
return formatJSONResponse({
message: `Hello, welcome to the exciting Serverless world!`,
});
};
PS: When initiating a get request, there is no need for a body request property which from my understanding is validated against the schema; so you can just specify undefined for the schema type.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.