Function file (hello.js)
Posted: Mon Feb 17, 2025 3:34 am
This is the JavaScript file that will be the main body of the serverless function.
A simple sample is written in the initial state, and the content has been slightly modified as follows.
// exports.main = (context, sendResponse) => {
// your code called when the function is executed
// const functionResponse = "Congrats! You've just deployed a Serverless Function."
// sendResponse is a callback function you call to send your response.
// sendResponse({body: functionResponse, statusCode: 200});
// }
const axios = require('axios');
exports.main = (context, sendResponse) => {
axios
.get
params: {
portalId: accountId,
term: 'searchTerm',
},
})
.then(function(response) {
console.log('Data received from the search API:', response.data);
sendResponse({ body: { response: response.data }, statusCode: 200 });
})
.catch(function(error) {
sendResponse({ body: { error: error.message }, statusCode: 500 });
});
};
Function file (hello.js)JavaScript
The important parts are main()the arguments, contextand sendResponse.
contextcontains various object keys. For example , contains context.bodythe data posted in JSON format, context.contactand contains information about the contact who made the request. This is a very important part because it contains various other data.
Next, sendResponseyou set the content of the response. You can set the body, statusCode, and headers if you want to redirect.
There are many aspects of serverless.json and function files that I cannot cover here, so please refer to the official website for more information .
Hello.js Improvements
As explained above, we will create a serverless function that will add characters and return the text when you enter and submit text in the website's input form, so we will change hello.js as follows.
exports.main = (context, sendResponse) => {
const responseBody = {
message: 'Hello!' + context.body.input
}
sendResponse({ body: armenia number data responseBody, statusCode: 200 });
};
hello.jsJavaScript
context.bodyinputIt is assumed that contains POST data sent from the website in JSON format .
inputThe processing involves returning Hello! to the message that was sent .
Creating and running a test page
To make testing easier, we will use the module [Rich Text].
Write the following content from the rich text source code editing.
<input id="input" type="text">
<button id="button">送信</button>
<div id="result">ここに結果を表示</div>
Rich TextHTML + HubL
The assumption is that the content entered in input will be sent and the response from the serverless function will be displayed in result.
A simple sample is written in the initial state, and the content has been slightly modified as follows.
// exports.main = (context, sendResponse) => {
// your code called when the function is executed
// const functionResponse = "Congrats! You've just deployed a Serverless Function."
// sendResponse is a callback function you call to send your response.
// sendResponse({body: functionResponse, statusCode: 200});
// }
const axios = require('axios');
exports.main = (context, sendResponse) => {
axios
.get
params: {
portalId: accountId,
term: 'searchTerm',
},
})
.then(function(response) {
console.log('Data received from the search API:', response.data);
sendResponse({ body: { response: response.data }, statusCode: 200 });
})
.catch(function(error) {
sendResponse({ body: { error: error.message }, statusCode: 500 });
});
};
Function file (hello.js)JavaScript
The important parts are main()the arguments, contextand sendResponse.
contextcontains various object keys. For example , contains context.bodythe data posted in JSON format, context.contactand contains information about the contact who made the request. This is a very important part because it contains various other data.
Next, sendResponseyou set the content of the response. You can set the body, statusCode, and headers if you want to redirect.
There are many aspects of serverless.json and function files that I cannot cover here, so please refer to the official website for more information .
Hello.js Improvements
As explained above, we will create a serverless function that will add characters and return the text when you enter and submit text in the website's input form, so we will change hello.js as follows.
exports.main = (context, sendResponse) => {
const responseBody = {
message: 'Hello!' + context.body.input
}
sendResponse({ body: armenia number data responseBody, statusCode: 200 });
};
hello.jsJavaScript
context.bodyinputIt is assumed that contains POST data sent from the website in JSON format .
inputThe processing involves returning Hello! to the message that was sent .
Creating and running a test page
To make testing easier, we will use the module [Rich Text].
Write the following content from the rich text source code editing.
<input id="input" type="text">
<button id="button">送信</button>
<div id="result">ここに結果を表示</div>
Rich TextHTML + HubL
The assumption is that the content entered in input will be sent and the response from the serverless function will be displayed in result.