AWS Amplify提供了几种不同的Lambda函数模板,每个模板都有不同的用途和参数。以下是三种常见的Lambda函数模板:
exports.handler = async (event, context) => {
const { httpMethod, body } = event;
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ message: "Hello World" })
};
if (httpMethod === 'GET') {
return response;
} else if (httpMethod === 'POST') {
// Process POST request body
return response;
}
};
const AmazonCognitoIdentity = require('amazon-cognito-identity-js');
global.fetch = require('node-fetch');
exports.handler = async (event) => {
const poolData = {
UserPoolId: process.env.USER_POOL_ID,
ClientId: process.env.APP_CLIENT_ID,
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
const { httpMethod, body } = event;
const response = {
statusCode: 200,
headers: {
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify({ message: "Hello World" })
};
if (httpMethod === 'GET') {
return response;
} else if (httpMethod === 'POST') {
// Verify user token
const token = event.headers.Authorization;
const cognitoUser = getTokenUser(userPool, token);
if (cognitoUser) {
// Process POST request body
return