### Install and Start Azure Function v4 Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/azure-http-function-v4/README.md Commands to navigate to the example directory, install dependencies, and start the local development server. ```bash cd examples/azure-http-function-v4 npm install npm run start ``` -------------------------------- ### Install Dependencies and Start UI Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Install project dependencies and start the UI in development or production mode. ```sh npm i npm run start:dev # or npm run start:prod ``` -------------------------------- ### Install and Run Example Locally Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-edge/README.md Commands to update the serverless-express dependency, install project dependencies, build the project, and run it locally for development. ```bash npm pack ../.. npm install ./codegenie-serverless-express-4.0.0-rc.1.tgz npm install npm run build npm run local ``` -------------------------------- ### Local Development Setup for Serverless Express Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/dynamodb-stream/README.md Use these commands to package the Serverless Express library, install it locally, and run the local development server. ```bash npm pack ../.. ``` ```bash npm install ./codegenie-serverless-express-4.0.0-rc.1.tgz ``` ```bash npm install --prefix ./src ./ ``` ```bash node local.js ``` -------------------------------- ### Local Development Setup for Serverless Express Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/alb/README.md Use these commands to package the Serverless Express library, install it locally, and run the local development server. Ensure you are in the correct directory before executing. ```bash npm pack ../.. ``` ```bash npm install ./codegenie-serverless-express-4.0.0-rc.3.tgz ``` ```bash npm install --prefix ./src ./ ``` ```bash node local.js ``` -------------------------------- ### Run Setup Script for Serverless Express App Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md This command installs Node.js dependencies, creates an S3 bucket if it doesn't exist, packages and deploys the serverless Express application to AWS Lambda, and creates an API Gateway proxy API. ```bash npm run setup ``` -------------------------------- ### Install Dependencies Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-nestjs/README.md Install the necessary project dependencies. ```bash $ npm install ``` -------------------------------- ### Clone and Navigate to Example Directory Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md Use these commands to clone the serverless-express repository and navigate to the basic starter API Gateway v2 example directory. ```bash git clone https://github.com/CodeGenieApp/serverless-express.git && cd serverless-express/examples/basic-starter-api-gateway-v2 ``` -------------------------------- ### Run Local API and UI Development Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Start the API and UI locally in separate terminals for rapid iteration. This setup uses cloud resources for database and authentication. ```sh npm run start-api:dev ``` ```sh npm run start-ui-local-api:dev ``` -------------------------------- ### Clone Serverless Express Example Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2-typescript/README.md Clone the serverless-express repository and navigate to the basic starter API example directory. ```sh git clone https://github.com/CodeGenieApp/serverless-express.git && cd serverless-express/examples/basic-starter-api-gateway-v2-typescript ``` -------------------------------- ### Initialize Developer Instance Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Run this command to deploy a developer instance to your AWS account, including dependency installation, credential setup, CDK bootstrapping, deployment, and local environment configuration. ```sh npm run init:dev ``` -------------------------------- ### Install Serverless Express Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-koa/README.md Install the serverless-express package into your existing project directory. ```bash npm install --save @codegenie/serverless-express ``` -------------------------------- ### Async Setup Lambda Handler (JavaScript) Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Use this pattern when your Lambda function needs to perform asynchronous bootstrap tasks, like database connections, before handling requests. It ensures setup runs only once. ```javascript // lambda.js require('source-map-support/register') const serverlessExpress = require('@codegenie/serverless-express') const app = require('./app') let serverlessExpressInstance function asyncTask () { return new Promise((resolve) => { setTimeout(() => resolve('connected to database'), 1000) }) } async function setup (event, context) { const asyncValue = await asyncTask() console.log(asyncValue) serverlessExpressInstance = serverlessExpress({ app }) return serverlessExpressInstance(event, context) } function handler (event, context) { if (serverlessExpressInstance) return serverlessExpressInstance(event, context) return setup(event, context) } exports.handler = handler ``` -------------------------------- ### Update Serverless Express Locally Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-koa/README.md Commands to update the example against the latest local changes to serverless-express. This involves packing the library, installing it locally, and running the local simulation. ```bash npm pack ../.. npm install ./codegenie-serverless-express-4.0.0-rc.1.tgz npm install --prefix ./src . npm run local ``` -------------------------------- ### Start UI in Development Mode Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Launch the UI locally for development after deploying to your AWS account. ```sh npm run start-ui:dev ``` -------------------------------- ### Install Serverless Express Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Install the serverless-express package using npm. This is the first step to integrate your Node.js web application with serverless platforms. ```bash npm install @codegenie/serverless-express ``` -------------------------------- ### Update local development environment Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md Commands to install local dependencies, build the project, and run a local simulation. ```bash npm i ../.. npm run build npm run local ``` -------------------------------- ### API Gateway Binary Media Types Configuration Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Example configuration for API Gateway's Binary Media Types in SAM. This should align with the `binarySettings` in Serverless Express. ```yaml ExpressApi: Type: AWS::Serverless::Api Properties: StageName: prod BinaryMediaTypes: ['image/*'] ``` -------------------------------- ### Azure Functions Core Tools Output Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/azure-http-function-v4/README.md Expected console output after successfully starting the local Azure Functions environment. ```text Azure Functions Core Tools Core Tools Version: 4.0.4590 Function Runtime Version: 4.5.2.18383 Functions: HttpExample: http://localhost:7071/api/{*segments} ``` -------------------------------- ### Delete S3 Bucket Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md If you specified a new S3 bucket during the setup and want to delete it, use this command. ```bash npm run delete-bucket ``` -------------------------------- ### Delete CloudFormation Stack Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md This command deletes the CloudFormation Stack, including the API and Lambda Function, that was created during the setup process. ```bash npm run delete-stack ``` -------------------------------- ### Bootstrap CDK for Production Environment Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/packages/cdk/README.md Run this command to bootstrap the AWS CDK for the 'todo_production' environment. This prepares your AWS account for CDK deployments. ```bash cdk bootstrap --profile todo_production ``` -------------------------------- ### Run Local Development Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-nestjs/README.md Simulate the application running on AWS Lambda locally. ```bash $ npm run sls:offline ``` -------------------------------- ### Bootstrap CDK for Development Environment Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/packages/cdk/README.md Run this command to bootstrap the AWS CDK for the 'todo_development' environment. This prepares your AWS account for CDK deployments. ```bash cdk bootstrap --profile todo_development ``` -------------------------------- ### Bootstrap CDK for Staging Environment Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/packages/cdk/README.md Run this command to bootstrap the AWS CDK for the 'todo_staging' environment. This prepares your AWS account for CDK deployments. ```bash cdk bootstrap --profile todo_staging ``` -------------------------------- ### Deploy to AWS Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-nestjs/README.md Deploy the application to AWS using the Serverless framework after configuring credentials. ```bash $ npm run sls:deploy ``` -------------------------------- ### Package and Deploy Node.js Project Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-koa/README.md Use this command to package and deploy your Node.js project after making updates to app.js. ```bash npm run package-deploy ``` -------------------------------- ### Deploy CDK Stack Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Bootstrap the CDK in your AWS account and deploy the application. This command may run deploy twice due to UI dependencies. ```sh npm run cdk-bootstrap:dev ``` ```sh npm run deploy:dev ``` -------------------------------- ### Deploy Serverless Express App Without Approval Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/packages/cdk/README.md Use this command to deploy the application when prompted for confirmation. It bypasses the 'Please confirm you intend to make the following modifications:' prompt. ```bash npm run deploy:dev -- --require-approval=never ``` -------------------------------- ### Local Simulation of API Gateway and Lambda Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-koa/README.md Update api-gateway-event.json with valid values and run this command for a local simulation of API Gateway and Lambda. ```bash npm run local ``` -------------------------------- ### Generate Serverless Express Project Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Use this command to generate a new serverless Express project with specified name, description, and deployment enabled. ```sh npx @codegenie/cli generate \ --name "Todo" \ --description "A todo list app that lets users create lists and add items to the list. Items should have a title, description, be markable as completed, have a due date, and have an image." --deploy ``` -------------------------------- ### Configure AWS Credentials for Initialization Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Customize AWS credential copying during `init:dev`. Use `COPY_AWS_PROFILE` to specify a profile to copy from, or set it to `0` to manually define the `todo_development` profile. ```sh COPY_AWS_PROFILE=profile-name-to-copy npm run init:dev ``` ```sh COPY_AWS_PROFILE=0 npm run init:dev ``` -------------------------------- ### Skip Web App Deployment Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/packages/cdk/README.md Include this context flag when deploying to skip the building and deployment of the web application. Useful for backend-only updates. ```bash --context skipDeployWebApp=true ``` -------------------------------- ### Configure UI API Endpoint Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Modify the UI's environment file to set the API endpoint. Change `NEXT_PUBLIC_ApiEndpoint` to one of the provided gateway, Lambda, or CloudFront URLs. ```sh NEXT_PUBLIC_ApiEndpoint ``` -------------------------------- ### Run Load Tests Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Execute load tests using one of the configured tools: autocannon or artillery. ```sh npm run [loadtest | loadtest:autocannon | loadtest:artillery] ``` -------------------------------- ### Configure Binary Response Settings Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Specify custom logic for determining if a response should be base64 encoded. Useful for images or compressed files. Ensure matching settings in API Gateway. ```javascript { binarySettings: { isBinary: ({ headers }) => true, contentTypes: ['image/*'], contentEncodings: [] } } ``` -------------------------------- ### Update Removed Options from v4 to v5 Source: https://github.com/codegenieapp/serverless-express/blob/mainline/UPGRADE.md The `resolutionMode` option is removed in v5.x, as only Promise-based resolution is supported. `binaryMimeTypes` is replaced by `binarySettings`. ```javascript // 4.x (deprecated) serverlessExpress({ app, resolutionMode: 'CALLBACK', // ❌ Removed binaryMimeTypes: ['image/*'] // ❌ Removed }) // 5.x serverlessExpress({ app, binarySettings: { contentTypes: ['image/*'] } }) ``` -------------------------------- ### Enable Error Stack Trace Reporting Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Set `respondWithErrors` to true to include error stack traces in responses for unhandled exceptions. Useful for development environments. ```javascript serverlessExpress({ app, respondWithErrors: true }) ``` -------------------------------- ### Migrate Lambda Handler from v3 to v4 Source: https://github.com/codegenieapp/serverless-express/blob/mainline/UPGRADE.md In v4.x, `binaryMimeTypes` is not required as the `isBase64Encoded` Lambda response value is automatically determined. For additional control, `binarySettings` can be specified. ```javascript // 3.x const awsServerlessExpress = require('aws-serverless-express') const app = require('./app') const binaryMimeTypes = [ 'image/*' ] const server = awsServerlessExpress.createServer(app, null, binaryMimeTypes) exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) } ``` ```javascript // 4.x const serverlessExpress = require('@codegenie/serverless-express') const app = require('./app') exports.handler = serverlessExpress({ app }) ``` ```javascript serverlessExpress({ app, binarySettings: { isBinary: ({ headers }) => true, contentTypes: [], contentEncodings: [] } }) ``` -------------------------------- ### Minimal Lambda Handler Wrapper (JavaScript) Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md This is the simplest way to integrate your Express app with AWS Lambda. Ensure your Express app is exported from './app.js'. ```javascript // lambda.js const serverlessExpress = require('@codegenie/serverless-express') const app = require('./app') exports.handler = serverlessExpress({ app }) ``` -------------------------------- ### Pull CloudFormation/CDK Outputs Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/lambda-function-url/README.md Re-run this command if the `cdk-outputs.json` file is missing or corrupted, often after a CloudFormation rollback. ```sh npm run pull-stack-outputs:dev ``` -------------------------------- ### Custom Logger Implementation Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Provide a custom logger object with 'info', 'debug', and 'error' methods to override the default logging behavior. This allows integration with external logging libraries like Winston. ```javascript { log: { info (message, additional) { console.info(message, additional) }, debug (message, additional) { console.debug(message, additional) }, error (message, additional) { console.error(message, additional) } } } ``` -------------------------------- ### Invoke Lambda Function Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/basic-starter-api-gateway-v2/README.md To invoke the Lambda function directly, first copy the LambdaFunctionName from the CloudFormation Outputs and paste it into the package.json config's functionName property. Then, run this command to invoke the Lambda Function with the payload specified in api-gateway-event.json. ```bash npm run invoke-lambda ``` -------------------------------- ### Update Handler Signature from v4 to v5 Source: https://github.com/codegenieapp/serverless-express/blob/mainline/UPGRADE.md In v5.x, the handler must be used with async/Promise patterns only and uses a default export. Version 4.x still supports callback-based patterns but they are no longer supported in v5.x. ```javascript // 4.x (still works but callback-based patterns no longer supported) exports.handler = serverlessExpress({ app }) // 5.x (same, but MUST be async/Promise-based) export default serverlessExpress({ app }) ``` -------------------------------- ### Custom Event Source Mapping Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Provide custom request and response mappers for integrating with event sources not natively supported by Serverless Express. Ensure your mappers correctly transform event data into Express-compatible request objects and vice versa. ```javascript function requestMapper ({ event }) { // Your logic here... return { method, path, headers } } function responseMapper ({ statusCode, body, headers, isBase64Encoded }) { // Your logic here... return { statusCode, body, headers, isBase64Encoded } } serverlessExpress({ app, eventSource: { getRequest: requestMapper, getResponse: responseMapper } }) ``` -------------------------------- ### Azure Function Configuration Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md This JSON defines the bindings for an Azure Function, crucial for Serverless Express integration. The '$return' out-binding is essential for proper operation. ```json // function.json { "bindings": [ { "authLevel": "anonymous", "type": "httpTrigger", "direction": "in", "name": "req", "route": "{*segments}" }, { "type": "http", "direction": "out", "name": "$return" } ] } ``` -------------------------------- ### Set Lambda Resolution Mode to Callback Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Configure Serverless Express to use the callback method for Lambda function resolution. This also sets `context.callbackWaitsForEmptyEventLoop = false`. ```javascript serverlessExpress({ app, resolutionMode: 'CALLBACK' }) ``` -------------------------------- ### Access Lambda Event and Context from v3 to v4 Source: https://github.com/codegenieapp/serverless-express/blob/mainline/UPGRADE.md In v4.x, `getCurrentInvoke` from `@codegenie/serverless-express` is used to access the Lambda event and context, replacing the `awsServerlessExpressMiddleware.eventContext()` middleware from v3.x. ```javascript // 3.x const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware') router.use(awsServerlessExpressMiddleware.eventContext()) router.get('/', (req, res) => { res.json({ stage: req.apiGateway.event.requestContext.stage }) }) ``` ```javascript // 4.x const { getCurrentInvoke } = require('@codegenie/serverless-express') router.get('/', (req, res) => { const currentInvoke = getCurrentInvoke() res.json({ stage: currentInvoke.event.requestContext.stage }) }) ``` -------------------------------- ### Event Source Routing Configuration Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Configure Serverless Express to route specific AWS event types (SNS, DynamoDB Streams, SQS, EventBridge) to designated Express routes. Events are typically POSTed to these routes. Ensure the 'Host' header matches the expected AWS service endpoint for security. ```yaml functions: lambda-handler: handler: src/lambda.handler events: - http: path: / method: get - sns: topicName: my-topic - stream: type: dynamodb arn: arn:aws:dynamodb:us-east-1:012345678990:table/my-table/stream/2021-07-15T15:05:51.683 - sqs: arn: arn:aws:sqs:us-east-1:012345678990:myQueue - eventBridge: pattern: source: - aws.cloudformation ``` ```javascript serverlessExpress({ app, eventSourceRoutes: { 'AWS_SNS': '/sns', 'AWS_DYNAMODB': '/dynamodb', 'AWS_SQS': '/sqs' 'AWS_EVENTBRIDGE': '/eventbridge', 'AWS_KINESIS_DATA_STREAM': '/kinesis', 'AWS_S3': '/s3', 'AWS_STEP_FUNCTIONS': '/step-functions', 'AWS_SELF_MANAGED_KAFKA': '/self-managed-kafka', } }) ``` ```javascript serverlessExpress({ app, eventSourceRoutes: { 'AWS_SNS': '/sns', } }) ``` -------------------------------- ### Log Level Configuration Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Configure the log level for the default logger in Serverless Express. Set to 'debug' for more verbose logging, or 'error' (default) for minimal logging. ```javascript { logSettings: { level: 'debug' // default: 'error' } } ``` -------------------------------- ### Azure Function v3/v4 Handler Wrapper Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md This JavaScript code wraps the Serverless Express application for use within an Azure Function. It requires the '@codegenie/serverless-express' package and your Express app definition. ```javascript // index.js const serverlessExpress = require('@codegenie/serverless-express') const app = require('./app') const cachedServerlessExpress = serverlessExpress({ app }) module.exports = async function (context, req) { return cachedServerlessExpress(context, req) } ``` -------------------------------- ### Update app.js for Serverless-Express Source: https://github.com/codegenieapp/serverless-express/blob/mainline/examples/sails-example/README.md Modify `app.js` to export a promise that resolves with the Sails.js HTTP app instance. This is necessary for simulating a Lambda handler invocation. ```javascript const appPromise = new Promise((resolve) => { sails.lift(rc('sails'), () => { resolve(sails.hooks.http.app) }) }) module.exports = appPromise ``` -------------------------------- ### Accessing Lambda Event and Context Objects Source: https://github.com/codegenieapp/serverless-express/blob/mainline/README.md Use the `getCurrentInvoke` function to retrieve the `event` and `context` objects provided by AWS Lambda. This is useful for inspecting incoming request details or Lambda runtime information. ```javascript const { getCurrentInvoke } = require('@codegenie/serverless-express') app.get('/', (req, res) => { const { event, context } = getCurrentInvoke() res.json(event) }) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.