zoukankan      html  css  js  c++  java
  • [AWS Lambda] Convert a Express node.js app to serverless

    Install new dependencies

    In a folder with the serverless project run the following commands to add new dependencies:

    npm install --save aws-serverless-express
    npm install --save express
    

    express - is a very popular Node.js web framework that we will use in this lesson aws-serverless-express - is a library that allows using express with AWS Lambda

    Import new dependencies

    In the getGroups.ts you need to import the following dependencies:

    import * as express from 'express'
    import * as awsServerlessExpress from 'aws-serverless-express'
    

    Create an Express instance

    Once the dependencies are imported you need to create an Express application:

    const app = express()
    

    Add a handler for a GET method

    To define how to process an incoming GET request, we need to use the app.get method and pass a function that will be called to process a request:

    app.get('/groups', async (_req, res) => {
      // TODO: get all groups as before
      const groups = ...
    
      // Return a list of groups
      res.json({
        items: groups
      })
    })
    

    You can read more about how to use Express here.

    To return a JSON response we use the .json() method on the response object.

    Export a Lambda handler

    Now the last thing that we need to do is to create a Lambda handler. To do this you can use the following code snippet:

    // Create Express server
    const server = awsServerlessExpress.createServer(app)
    // Pass API Gateway events to the Express server
    exports.handler = (event, context) => { awsServerlessExpress.proxy(server, event, context) }
    

     

  • 相关阅读:
    群发邮件2
    谈谈C#中的三个关键词new , virtual , override
    一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
    网站静态化结构
    第四十七章 天神的邀请
    asp.net 异步群发邮件时遭遇到的问题 ddddddddd
    第四十章 远方的消息
    商用群发p2p网络
    第四十八章 三大客卿
    第四十五章 你没让我失望
  • 原文地址:https://www.cnblogs.com/Answer1215/p/14922673.html
Copyright © 2011-2022 走看看