Serverless 入門(四)- 如何除錯
上兩篇文章,我們講了如何在 Terminal 裡呼叫服務,只需要輸入: serverless invoke -f hello -l -d Kenny鍋
那麼問題來了,我們要除錯時,總不能每次都要部署到 AWS 服務端吧,這樣效率比較低!我們能在本地除錯好了,最後部署到 AWS 上嗎? 答案是: 可以!
1.使用 Terminal 除錯
這個比較簡單,只需在 invoke 後加上 local
就行,具體寫法: serverless invoke local -f hello -l -d Kenny鍋
就算是這麼除錯,好像也不太方便,如果能在 Insomnia、Postman 之類的工具除錯就好了,當然沒問題。但有會稍微麻煩一點點。只要跟我下面的步驟,你 5 分鐘就能完成。
2. 使用工具除錯
2.1 安裝 serverless-offline
在 Terminal 裡執行: yarn add serverless-offline -D
2.2 修改 serverless.yml
開啟根目錄下的 serverless.yml
,在 handler:
那行下面新增如下配置:
events: - http: path: hello/{name} method: get plugins: - serverless-offline
為了減少大家出錯的情況,這裡給出全部 serverless.yml 內容:
service: hello-world provider: name: aws runtime: nodejs8.10 functions: hello: handler: index.greeting events: - http: path: hello/{name} method: get plugins: - serverless-offline
2.3 修改 js 檔案
新增取值的程式碼,見:
const {pathParameters = {}} = event; const {name = '無名女屍'} = pathParameters; const message = `你好,${ name }.`;
index.js 完整程式碼,見:
const greeting = async (event, context) => { const {pathParameters = {}} = event; const {name = '無名女屍'} = pathParameters; const message = `你好,${ name }.`; return { statusCode: 200, body: JSON.stringify({ message }), }; }; module.exports = { greeting };
為什麼能接收 name 呢? 因為我們在 serverless.yml 的 path 裡配置這個路由,然後就可以在 js 裡取值。 順便提一下 event 和 context 引數吧:
- event 物件裡有用的值並不多,見:
{ headers: { Host: 'localhost:3000', 'User-Agent': 'insomnia/6.3.2', Accept: '*/*' }, multiValueHeaders: { Host: [ 'localhost:3000' ], 'User-Agent': [ 'insomnia/6.3.2' ], Accept: [ '*/*' ] }, path: '/hello/Kenny', pathParameters: { name: 'Kenny' }, requestContext: { accountId: 'offlineContext_accountId', resourceId: 'offlineContext_resourceId', apiId: 'offlineContext_apiId', stage: 'dev', requestId: 'offlineContext_requestId_6815488628284807', identity: { cognitoIdentityPoolId: 'offlineContext_cognitoIdentityPoolId', accountId: 'offlineContext_accountId', cognitoIdentityId: 'offlineContext_cognitoIdentityId', caller: 'offlineContext_caller', apiKey: 'offlineContext_apiKey', sourceIp: '127.0.0.1', cognitoAuthenticationType: 'offlineContext_cognitoAuthenticationType', cognitoAuthenticationProvider: 'offlineContext_cognitoAuthenticationProvider', userArn: 'offlineContext_userArn', userAgent: 'insomnia/6.3.2', user: 'offlineContext_user' }, authorizer: { principalId: 'offlineContext_authorizer_principalId', claims: undefined }, protocol: 'HTTP/1.1', resourcePath: '/hello/{name}', httpMethod: 'GET' }, resource: '/hello/{name}', httpMethod: 'GET', queryStringParameters: null, multiValueQueryStringParameters: null, stageVariables: null, body: null, isOffline: true }
- context 裡有用的東西就更少了,見:
{ done: [Function], succeed: [Function: succeed], fail: [Function: fail], getRemainingTimeInMillis: [Function: getRemainingTimeInMillis], functionName: 'hello-world-dev-hello', memoryLimitInMB: undefined, functionVersion: 'offline_functionVersion_for_hello-world-dev-hello', invokedFunctionArn: 'offline_invokedFunctionArn_for_hello-world-dev-hello', awsRequestId: 'offline_awsRequestId_7983077759511026', logGroupName: 'offline_logGroupName_for_hello-world-dev-hello', logStreamName: 'offline_logStreamName_for_hello-world-dev-hello', identity: {}, clientContext: {} }
為了讓大家跟我的目錄保持一致,下面列出我的專案結構:
├── index.js ├── node_modules ├── package.json ├── serverless.yml └── yarn.lock
2.4 啟動服務
現在來試試吧,在 Terminal 裡執行: sls offline
。如果啟動成功,會在3000 埠上啟動服務,見:
Serverless: Starting Offline: dev/us-east-1. Serverless: Routes for hello: Serverless: GET /hello/{name} Serverless: Offline listening on http://localhost:3000
這個時候,我們就可以在 Insomnia 裡輸入: http://localhost:3000/hello/Kenny ,見:

因為是 get 請求,所以用瀏覽器也可以開啟。
2.5 自動過載程式碼
我們總是想盡一切辦法提升工作、開發效率,比如 webpack 和 nodemon 有 reload 的功能,當然 serverless-offline 也有。
先賣個關子,如果到 10 個贊,我就放出來這個技巧,能讓你提前 30 分鐘下班 ^ _ ^
相關文章
- Serverless 入門(一) - 建立 IAM https://www.jianshu.com/p/9fb731a799e2
- Serverless 入門(二) - HelloWord https://www.jianshu.com/p/ddf2ffda5f63
- Serverless 入門(三)- 初始專案解讀 https://www.jianshu.com/p/8baba2a8fe9f
- Serverless 入門(四)- 如何除錯 https://www.jianshu.com/p/58d30915de8a