在上篇文章中,咱們講解了如何調用咱們的hello-world應用,只須要使用命令:html
serverless invoke -f hello -l
,可是咱們總不可能修改一次代碼,就調用一下這個命令吧,或者說咱們須要調試咱們的代碼的時候,總不能每次都要部署到AWS服務器端吧,那麼這樣的效率很是低。所以咱們須要在本地調式完成後,咱們最後部署到咱們的AWS服務器上便可。node
1. 使用 Terminal調式webpack
咱們只須要在 invoke 後加 local 就能夠了,如命令:serverless invoke local -f hello -l 以下所示:git
可是如上調式,也不是最好的方案,所以咱們須要使用工具調試就行了。爲了解決這個問題,在serverless中也有相似的工具。github
2. 使用工具調試web
2.1 安裝 serverless-offline 命令以下所示:npm
npm install serverless-offline -D
2.2 修改 serverless.ymlapi
咱們須要打開咱們的根目錄下的 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 代碼,而後會命令行中會自動打包,可是命令行中不會有任何提示,可是當咱們刷新頁面的時候,發現內容是最新的了以下所示: