如何使用AWS SAM微服务,nodejs在项目中组织文件?

问题描述

我需要具有以下堆栈的API:具有2个微服务的AWS SAM和node.js:

  • users
  • books

我做了什么:

  1. sam init
  2. AWS快速入门模板
  3. nodejs12.x
  4. 将项目命名为books

现在我有以下模板:

AWstemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  books

  Sample SAM Template for books
  
# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello-world/
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

Outputs:
  # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function
  # Find out more about other implicit resources you can reference within SAM
  # https://github.com/awslabs/serverless-application-model/blob/master/docs/internals/generated_resources.rst#api
  HelloWorldApi:
    Description: "API Gateway endpoint URL for Prod stage for Hello World function"
    Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/"
  HelloWorldFunction:
    Description: "Hello World Lambda Function ARN"
    Value: !GetAtt HelloWorldFunction.Arn
  HelloWorldFunctionIamRole:
    Description: "Implicit IAM Role created for Hello World function"
    Value: !GetAtt HelloWorldFunctionRole.Arn

我的三个文件如下:

?app
 ┗ ?books
 ┃ ┣ ?events
 ┃ ┃ ┗ ?event.json
 ┃ ┣ ?hello-world
 ┃ ┃ ┣ ?tests
 ┃ ┃ ┃ ┗ ?unit
 ┃ ┃ ┃ ┃ ┗ ?test-handler.js
 ┃ ┃ ┣ ?.npmignore
 ┃ ┃ ┣ ?app.js
 ┃ ┃ ┗ ?package.json
 ┃ ┣ ?.gitignore
 ┃ ┣ ?README.md
 ┃ ┗ ?template.yaml

我对这种无用的服务还是陌生的,我对express.js和微服务有一定的经验,所以我的问题...

如何组织文件以提供微服务? 我必须制作几个模板吗?

?app
 ┗ ?books
 ┃ ┣ ?events
 ┃ ┃ ┗ ?event.json
 ┃ ┣ ?hello-world
 ┃ ┃ ┣ ?tests
 ┃ ┃ ┃ ┗ ?unit
 ┃ ┃ ┃ ┃ ┗ ?test-handler.js
 ┃ ┃ ┣ ?.npmignore
 ┃ ┃ ┣ ?app.js
 ┃ ┃ ┗ ?package.json
 ┃ ┣ ?.gitignore
 ┃ ┣ ?README.md
 ┃ ┗ ?template.yaml
 ┗ ?users
 ┃ ┣ ?events
 ┃ ┃ ┗ ?event.json
 ┃ ┣ ?hello-world
 ┃ ┃ ┣ ?tests
 ┃ ┃ ┃ ┗ ?unit
 ┃ ┃ ┃ ┃ ┗ ?test-handler.js
 ┃ ┃ ┣ ?.npmignore
 ┃ ┃ ┣ ?app.js
 ┃ ┃ ┗ ?package.json
 ┃ ┣ ?.gitignore
 ┃ ┣ ?README.md
 ┃ ┗ ?template.yaml

我非常感谢您能提供的任何帮助。我会很感激。我了解这一理论,但是在如何订购文件方面找不到任何帮助。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)