在上篇文章中,我們講解了如何調用我們的hello-world應用,只需要使用命令:
serverless invoke -f hello -l
,但是我們總不可能修改一次代碼,就調用一下這個命令吧,或者說我們需要調試我們的代碼的時候,總不能每次都要部署到AWS服務器端吧,那么這樣的效率非常低。因此我們需要在本地調式完成后,我們最后部署到我們的AWS服務器上即可。
1. 使用 Terminal調式
我們只需要在 invoke 后加 local 就可以了,如命令:serverless invoke local -f hello -l 如下所示:
但是如上調式,也不是最好的方案,因此我們需要使用工具調試就好了。為了解決這個問題,在serverless中也有類似的工具。
2. 使用工具調試
2.1 安裝 serverless-offline 命令如下所示:
npm install serverless-offline -D
2.2 修改 serverless.yml
我們需要打開我們的根目錄下的 serverless.yml, 添加如下配置信息:
events: - http: path: hello/{name} method: get plugins: - serverless-offline
因此 serverless.yml 所有配置代碼如下:
service: hello-world provider: name: aws runtime: nodejs10.x functions: hello: handler: handler.hello events: - http: path: hello/{name} method: get plugins: - serverless-offline
2.3 修改handler.js
現在我們在handler.js 中添加如下代碼:
const {pathParameters = {}} = event; const {name = 'xxx111'} = pathParameters; const message = `您好,${ name }.`;
handler.js 的完整的代碼如下所示:
'use strict'; module.exports.hello = async (event, context, callback) => { console.log(event); console.log(context); const {pathParameters = {}} = event; const {name = 'xxx111'} = pathParameters; const message = `您好,${ name }.`; const html = ` <html> <style> h2 {color:red;} </style> <body> <h1>第一個hello world 應用</h1> <h2>${message}</h2> </body> </html>`; return { statusCode: 200, headers: { 'Content-Type': 'text/html' }, body: html } };
如上代碼打印,我們打印 console.log(event); 后,我們打印的信息如下所示:
{ headers: { Host: 'localhost:3000', Connection: 'keep-alive', Pragma: 'no-cache', 'Cache-Control': 'no-cache', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36', Accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8' }, multiValueHeaders: { Host: [ 'localhost:3000' ], Connection: [ 'keep-alive' ], Pragma: [ 'no-cache' ], 'Cache-Control': [ 'no-cache' ], 'Upgrade-Insecure-Requests': [ '1' ], 'User-Agent': [ 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36' ], Accept: [ 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3' ], 'Accept-Encoding': [ 'gzip, deflate, br' ], 'Accept-Language': [ 'zh-CN,zh;q=0.9,en;q=0.8' ] }, path: '/hello/2', pathParameters: { name: '2' }, requestContext: { accountId: 'offlineContext_accountId', resourceId: 'offlineContext_resourceId', apiId: 'offlineContext_apiId', stage: 'dev', requestId: 'offlineContext_requestId_03349087551215857', 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: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36', user: 'offlineContext_user' }, authorizer: { principalId: 'offlineContext_authorizer_principalId', claims: undefined }, protocol: 'HTTP/1.1', resourcePath: '/hello/{name}', httpMethod: 'GET', requestTimeEpoch: 1561535723603 }, resource: '/hello/{name}', httpMethod: 'GET', queryStringParameters: null, multiValueQueryStringParameters: null, stageVariables: null, body: null, isOffline: true }
然后我們打印我們的 console.log(context); 后,打印的信息如下所示:
{ done: [Function], fail: [Function: fail], succeed: [Function: succeed], getRemainingTimeInMillis: [Function: getRemainingTimeInMillis], awsRequestId: 'offline_awsRequestId_7749009079208731', clientContext: {}, functionName: 'hello-world-dev-hello', functionVersion: 'offline_functionVersion_for_hello-world-dev-hello', identity: {}, invokedFunctionArn: 'offline_invokedFunctionArn_for_hello-world-dev-hello', logGroupName: 'offline_logGroupName_for_hello-world-dev-hello', logStreamName: 'offline_logStreamName_for_hello-world-dev-hello', memoryLimitInMB: undefined }
2.4 啟動服務
如上配置代碼完成后,我們可以通過命令:sls offline 來運行了,如果啟動成功,我們就會綁定3000端口了,如下所示:
這個時候,我們在瀏覽器 http://localhost:3000/hello/2 訪問的時候,可以看到如下信息了;如下所示:
當我們把上面的地址改下的話,比如 http://localhost:3000/hello/kongzhi, 那么頁面變成如下了,如下所示:
2.5 修改保存后自動加載代碼
我們總是想盡一切辦法提升工作、開發效率,比如 webpack 和 nodemon 有 reload 的功能,當然 serverless-offline 也有。運行命令如下:
sls offline --useSeparateProcesses
如下所示:
現在當我修改了下 handler.js 代碼,然后會命令行中會自動打包,但是命令行中不會有任何提示,但是當我們刷新頁面的時候,發現內容是最新的了如下所示: