有人能阐明我在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
}
我的Express服务器文件(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
答案 0 :(得分:2)
确保将context.callbackWaitsForEmptyEventLoop
设置为false
在这里https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html
了解更多信息答案 1 :(得分:0)
问题与我的测试工具有关
https://www.npmjs.com/package/run-local-lambda
当我与AWS支持人员交谈时,他们告诉我使用:
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
}
然后我的代码执行得很好,没有超时。