运行 serverless invoke test
并使用 Vandium 时,我收到 Status Code 500
和错误:
{"type":"Error","message":"Expected event type of apigateway but identified as unknown”}
这是重现问题的代码。首先,工作示例(没有 Vandium):
time/handler.js:
'use strict';
module.exports.endpoint =
(event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: `Hello, the current time is ${new Date().toTimeString()}.`,
}),
};
callback(null, response);
}
serverless.yml:
service: serverless-starter
provider:
name: aws
runtime: nodejs6.10
functions:
currentTime:
handler: time/handler.endpoint
events:
- http:
path: ping
method: get
plugins:
- serverless-offline
- serverless-mocha-plugin
最后,自动生成检查状态码的测试代码: 测试/currentTime.js:
'use strict';
// tests for currentTime
// Generated by serverless-mocha-plugin
const mochaPlugin = require('serverless-mocha-plugin');
const expect = mochaPlugin.chai.expect;
let wrapped = mochaPlugin.getWrapper('currentTime', '/time/handler.js', 'endpoint');
describe('currentTime', () => {
before((done) => {
done();
});
it('implement tests here', () => {
return wrapped.run({}).then((response) => {
const body = JSON.parse(response.body);
expect(response.statusCode).to.be.equal(200);
});
});
});
这很好用。我可以启动服务器:serverless offline start
并且我能够获得 http://localhost:3000/ping端点可以用 curl 或 POSTMAN 处理。
如果我运行 serverless invoke test
那么测试运行正常。到目前为止没有问题。
现在,当我重构代码并使用 Vandium 时: time/handler.js:
'use strict';
const vandium = require('vandium');
module.exports.endpoint = vandium.api()
.GET((event) => {
return {
"id": "12345",
"name": "john.doe"
};
}
)
然后一切正常,即我可以启动服务器并使用 curl 或 POSTMAN 连接到端点。
但是,当我再次运行测试时 serverless invoke test
然后我得到 500 和错误,而不是状态代码 200:
{"type":"Error","message":"expected event type of apigateway but identified as unknown”}
怎么了?
最佳答案
使用 APIGatewayEvent 对象或支持的 aws 事件之一创建一个 json 文件。然后使用选项 -p 或 -d 运行无服务器(参见:https://serverless.com/framework/docs/providers/aws/cli-reference/invoke-local#options)
sls invoke local -f hello -p path-to-json.json
看看https://github.com/vandium-io/lambda-event-identifier/blob/master/lib/identify.js了解更多信息
关于node.js - 使用 Vandium : Expected event type of apigateway but identified as unknown 时无服务器调用测试返回状态 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221827/