前端技术

【Serverless 极速指南】解读项目

2019-08-20  本文已影响2人  一俢

Serverless 模版创建出来的项目的文件内容特别的少,只有几个,但是麻雀虽小五脏俱全,我们还是认真的 Review 一遍:

这篇文章你会学习到:

.gitignore

.gitignore 这个文件大家非常熟悉,它是 GIT 的忽略文件,想必 Serverless 也是将软件开源的思想,还是一行一行解读:

# package directories
node_modules
jspm_packages

# Serverless directories
.serverless

handler.js

handler.js 是有 Serverless 生成的一段程序模版,它会直接接入 Serverless ,并且输出 { message: 'Go Serverless v1.0! Your function executed successfully!', event }

'use strict';

module.exports.hello = async event => {
    return {
        statusCode: 200,
        body: JSON.stringify(
            {
                message: 'Go Serverless v1.0! Your function executed successfully!',
                input: event,
            },
            null,
            2
        ),
    };

    // Use this code if you don't use the http event with the LAMBDA-PROXY integration
    // return { message: 'Go Serverless v1.0! Your function executed successfully!', event };
};

继续解读代码:

OK,其实这段代码是将业务逻辑和 Serverless 结合的地方,Serverless 会调用这个 hello 方法,而 hello 方法内部就是我们要实现的业务逻辑。

serverless.yml

serverless.yml 是整个 Serverless 项目的描述文件或者配置文件:

service: hello-world
provider:
  name: aws
  runtime: nodejs10.x  
  stage: dev
  region: cn-north-1
functions:
  hello:
    handler: handler.hello

YAML 是专门编写配置文件的语言,了解它的语法可以自行百度。

这部分的配置非常多,我们只讲一部分:

注:中国区的项目一定要注明区域

.serverless 文件夹

当你运行 serverless deploy 部署命令之后,项目会生成一个 .serverless 文件夹,它将会部署到 Serverless 中去:

├── .serverless
│   ├── cloudformation-template-create-stack.json
│   ├── cloudformation-template-update-stack.json
│   ├── hello-world.zip
│   └── serverless-state.json

这部分内容,暂时不做详细的解释,暂时我们用不到。

最后,其实这个模版项目也只能做一个简单的 HellWorld,在复杂的工程项目中,我们需要更多功能强大的 Serverless 模块才能完成,通过那些模块,让整个项目的代码更加有层次、开发体验更加的好,我们会在后面的实战运用中讲到。

〖坚持的一俢〗

上一篇 下一篇

猜你喜欢

热点阅读