A touch to serverless framework – final part
- 2021年7月12日
- Web Service
We have created our serverless endpoint last week and lets tweak a bit more today by adding some parameters to our endpoint. Lets get started.
We don’t have to modify serverless.yml and lets keep the same way we did before.
events:
- http:
path: sample-endpoint
method: GET
Lets modify the handler.js
as we are adding a name
parameter to our endpoint and get a different response depending on the input parameter. We will add a conditional statement for our name
parameter in our handler.js like this.
if (event.queryStringParameters && event.queryStringParameters.name) {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello' + event.queryStringParameters.name,
input: event,
},
null,
2
),
};
}
So this will be our final result of handler.js
'use strict';
module.exports.hello = async (event) => {
if (event.queryStringParameters && event.queryStringParameters.name) {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Hello' + event.queryStringParameters.name,
input: event,
},
null,
2
),
};
}
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
},
null,
2
),
}
};
Lets deploy our code and see the result.
Lets go to the endpoint from the browser but this time we will add a name
parameter at the end of the url like this
?name=Hlaing
As a final result, we will get a response like this.
You can see the result of our name parameter in the json response of message data.
That’s all for today and we have successfully added a new parameter to our endpoint.
As a conclusion note, I have to say we can still do many things through this serverless project. We can also create other method type endpoint like POST. We can also create services like S3 buckets, DBs and so on through serverless.yml and deploying it. But let me stop the article series here as I also intended to get you a starting point to the serverless framework and I think you got it.
See you again . Yuuma
yuuma at 2021年07月12日 11:00:25