AWS-serverless-express 从不通过承诺解决

Posted

技术标签:

【中文标题】AWS-serverless-express 从不通过承诺解决【英文标题】:AWS-serverless-express never resolving with promises 【发布时间】:2020-06-29 23:01:46 【问题描述】:

谁能阐明我在 github 上的 aws-serverless-express 存储库中打开的问题? https://github.com/awslabs/aws-serverless-express/issues/276

我正在尝试使用 aws-serverless-express 包运行我以前的 express.js 服务器。当我在没有任何特殊选项的情况下运行时,我解决了,但链中的承诺永远不会得到尊重,这意味着我不会在事件循环中执行所有事情。

如果我使用“PROMISE”标志运行 serverlessexpress,我会执行我的所有承诺,但程序永远不会解析并在设置的最长时间后超时。

我什至按照该仓库中的示例启动了一个新项目,结果相同。

解决但不遵守我的承诺时我的主要执行文件(index.js)

const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)

exports.handler = (event, context) => 
  return awsServerlessExpress.proxy(server, event, context)

我的主要执行文件 (index.js) 未解决,但尊重我的承诺

const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)

exports.handler = (event, context) => 
  return awsServerlessExpress.proxy(server, event, context, 'PROMISE')

我也试过这个:

const awsServerlessExpress = require('aws-serverless-express')
const app = require('./app.js')
const server = awsServerlessExpress.createServer(app, null)

exports.handler = (event, context) => 
  return awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise

我的快递服务器文件(app.js)

const express = require('express')
const bodyParser = require('body-parser')
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware')
const app = express()
const router = express.Router()

router.use(bodyParser.json())
router.use(bodyParser.urlencoded( extended: true ))
router.use(awsServerlessExpressMiddleware.eventContext())

router.get('/', (req, res) => 
  res.render('index', 
    apiUrl: req.apiGateway ? `https://$req.apiGateway.event.headers.Host/$req.apiGateway.event.requestContext.stage` : 'http://localhost:3000'
  )
)

router.get('/users', (req, res) => 
  res.json(users)
)

const users = [
  id: 1,
  name: 'Joe'
, 
  id: 2,
  name: 'Jane'
]

function myFunc () 
  console.log('hey')

setTimeout(myFunc, 3000)

app.use('/', router)

module.exports = app

【问题讨论】:

This Github issue comment 会建议它是 awsServerlessExpress.proxy(server, event, context, 'PROMISE').promise 而不是 awsServerlessExpress.proxy(server, event, context, 'PROMISE') 这可能会起作用。线程中的下一条评论还提出了另一种解决方案 - 我希望它有所帮助。 @cubrr 我也试过了,结果一样。 【参考方案1】:

问题与我的测试工具有关:

https://www.npmjs.com/package/run-local-lambda

当我与 AWS Support 交谈时,他们告诉我使用:

sam local invoke "Test" -e event.json

使用包含以下内容的 event.json 文件:


    "httpMethod": "OPTIONS",
    "//body": "\"name\": \"Sam\"",
    "path": "/api/auth",
    "resource": "/proxy+",
    "queryStringParameters": ,
    "pathParameters": 
        "proxy": "users"
    ,
    "headers": 
        "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
        "Accept-Encoding": "gzip, deflate, sdch, br",
        "Accept-Language": "en-US,en;q=0.8",
        "CloudFront-Forwarded-Proto": "https",
        "CloudFront-Is-Desktop-Viewer": "true",
        "CloudFront-Is-Mobile-Viewer": "false",
        "CloudFront-Is-SmartTV-Viewer": "false",
        "CloudFront-Is-Tablet-Viewer": "false",
        "CloudFront-Viewer-Country": "US",
        "Content-Type": "application/json",
        "Host": "xxxxxxxxxx.execute-api.us-east-1.amazonaws.com",
        "Upgrade-Insecure-Requests": "1",
        "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
        "Via": "1.1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.cloudfront.net (CloudFront)",
        "X-Amz-Cf-Id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_xxxxxxxxxxx_xxxx==",
        "X-Forwarded-For": "11.111.111.111, 11.111.111.111",
        "X-Forwarded-Port": "111",
        "X-Forwarded-Proto": "http",
        "x-apigateway-event": "hej"

    ,
    "requestContext": 
        "accountId": "111111111111",
        "resourceId": "xxxxxx",
        "stage": "prod",
        "requestId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
        "identity": 
            "cognitoIdentityPoolId": "",
            "accountId": "",
            "cognitoIdentityId": "",
            "caller": "",
            "apiKey": "",
            "sourceIp": "11.111.111.111",
            "cognitoAuthenticationType": "",
            "cognitoAuthenticationProvider": "",
            "userArn": "",
            "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36",
            "user": ""
        ,
        "resourcePath": "/proxy+",
        "httpMethod": "GET",
        "apiId": "xxxxxxxxxx"
    

还有一个仅用于测试的模板文件,如下所示:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Description:
 A test project

Resources:
 Test:
  Type: AWS::Serverless::Function
  Properties:
   Runtime: nodejs10.x
   Handler: index.handler
   Timeout: 10
   Environment:
     Variables:
       NODE_ENV: "test"
       DB_NAME: "dbname"
       DB_USER: "dbuser"
       DB_PASSWORD: "secret"
       DB_URL: "dburl"

我在处理程序中使用的代码如下所示:

'use strict'
const awsServerlessExpress = require('aws-serverless-express')
const app = require('./main.js')

const server = awsServerlessExpress.createServer(app, null)    
exports.handler = (event, context) => 
  return awsServerlessExpress.proxy(server, event, context,'PROMISE').promise

然后我的代码执行得非常好,从来没有超时。

【讨论】:

【参考方案2】:

确保将 context.callbackWaitsForEmptyEventLoop 设置为 false

在此处阅读更多信息https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html

【讨论】:

以上是关于AWS-serverless-express 从不通过承诺解决的主要内容,如果未能解决你的问题,请参考以下文章

请求正文未从 aws-serverless-express 模块进入 app.js 文件

Express GraphQL 必须提供查询字符串。

在内存警告后调用 viewdidunload 后视图从不重新加载(viewdidload 从不调用)

读:只要我还在爱,我从不主动离开,如果我不爱了,我从不留恋!

wpf 从不同方向看一个model?比如,从正面,方面,上面等方向 谁有这方面的demo,发给我邮箱,万分感谢

为啥 fusedLocationProviderClient.getLastLocation 从不调用 OnSuccessListener?