我正在按照这个基本示例(https://medium.com/hackernoon/tdd-means-zen-along-with-serverless-means-nirvana-a39a76ee8e63)设置 serverless-offline 以对无服务器项目进行单元测试。
不幸的是,我无法让 serverless-offline 的初始运行正常工作。运行“serverless offline start”后,控制台输出不显示 GET 方法,对 localhost:3000 的请求仅返回“Cannot GET /”。
有什么想法我在这里想念的吗?提前致谢。
我的 serverless.yml:
provider:
name: aws
runtime: nodejs10.x
functions:
hello:
handler: handler.hello
events:
- http:
path: ''
method: get
plugins:
- serverless-offline
- serverless-mocha-plugin
我的 package.json:
"name": "my-api",
"version": "1.0.0",
"description": "",
"main": "handler.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"mongodb": "^3.3.3"
},
"devDependencies": {
"serverless-mocha-plugin": "^1.11.0",
"serverless-offline": "^5.12.0"
}
}
我的 handler.js:
module.exports.hello = async event => {
return {
statusCode: 200,
body: JSON.stringify(
{
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event
},
null,
2
)
};
};
控制台输出:
$ sls offline start
Serverless: Load command interactiveCli
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command config:tabcompletion
Serverless: Load command config:tabcompletion:install
Serverless: Load command config:tabcompletion:uninstall
Serverless: Load command create
Serverless: Load command install
Serverless: Load command package
Serverless: Load command deploy
Serverless: Load command deploy:function
Serverless: Load command deploy:list
Serverless: Load command deploy:list:functions
Serverless: Load command invoke
Serverless: Load command invoke
Serverless: Load command invoke:local
Serverless: Load command info
Serverless: Load command logs
Serverless: Load command metrics
Serverless: Load command print
Serverless: Load command remove
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command slstats
Serverless: Load command plugin
Serverless: Load command plugin
Serverless: Load command plugin:install
Serverless: Load command plugin
Serverless: Load command plugin:uninstall
Serverless: Load command plugin
Serverless: Load command plugin:list
Serverless: Load command plugin
Serverless: Load command plugin:search
Serverless: Load command config
Serverless: Load command config:credentials
Serverless: Load command rollback
Serverless: Load command rollback:function
Serverless: Load command offline
Serverless: Load command offline:start
Serverless: Load command create
Serverless: Load command create:test
Serverless: Load command create:function
Serverless: Load command invoke
Serverless: Load command invoke:test
Serverless: Load command login
Serverless: Load command logout
Serverless: Load command generate-event
Serverless: Load command test
Serverless: Load command dashboard
Serverless: Invoke offline:start
Serverless: Starting Offline: dev/us-east-1.
[offline] options: { apiKey: 'XXXXXXXXXXXXXXXXXX',
cacheInvalidationRegex: /node_modules/,
corsAllowOrigin: [ '*' ],
corsAllowCredentials: true,
corsAllowHeaders: [ 'accept', 'content-type', 'x-api-key', 'authorization' ],
corsExposedHeaders: [ 'WWW-Authenticate', 'Server-Authorization' ],
`
disableCookieValidation: false,
enforceSecureCookies: false,
exec: '',
hideStackTraces: false,
host: 'localhost',
httpsProtocol: '',
location: '.',
noAuth: false,
noEnvironment: false,
noTimeout: false,
port: 3000,
prefix: '/',
preserveTrailingSlash: false,
printOutput: false,
providedRuntime: '',
showDuration: false,
stage: 'dev',
region: 'us-east-1',
resourceRoutes: false,
skipCacheInvalidation: false,
useSeparateProcesses: false,
websocketPort: 3001,
corsConfig:
{ credentials: true,
exposedHeaders: [ 'WWW-Authenticate', 'Server-Authorization' ],
headers: [ 'accept', 'content-type', 'x-api-key', 'authorization' ],
origin: [ '*' ] } }
[offline] funOptions {
"funName": "hello",
"funTimeout": 30000,
"handlerName": "hello",
"handlerPath": "/Users/rocky/Documents/Projects/garmin/sls-troubleshooting/my-api/ha
ndler",
"runtime": "nodejs10.x"
}
[offline] hello runtime nodejs10.x
Serverless: Routes for hello:
[offline] Response Content-Type application/json
Serverless: POST /{apiVersion}/functions/my-api-dev-hello/invocations
Serverless: Offline [HTTP] listening on http://localhost:3000
Serverless: Enter "rp" to replay the last request