在上篇文章中,咱们讲解了如何调用咱们的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 代码,而后会命令行中会自动打包,可是命令行中不会有任何提示,可是当咱们刷新页面的时候,发现内容是最新的了以下所示: