服务器到服务器Node.js应用程序

AEM Headlessas a Cloud Service

示例应用程序是探索Adobe Experience Manager (AEM)的Headless功能的绝佳方法。 此服务器到服务器应用程序演示了如何使用AEM GraphQL API通过持久查询来查询内容并在终端上打印。

带有AEM Headless的服务器到服务器Node.js应用程序

查看 GitHub上的源代码

前提条件 prerequisites

应在本地安装以下工具:

AEM要求

Node.js应用程序可与以下AEM部署选项配合使用。 所有部署都需要 WKND站点v3.0.0+ 即将安装。

此Node.js应用程序可以根据命令行参数连接到AEM Author或AEM Publish。

使用方法

  1. 克隆 adobe/aem-guides-wknd-graphql 存储库:

    code language-shell
    $ git clone git@github.com:adobe/aem-guides-wknd-graphql.git
    
  2. 打开终端并运行以下命令:

    code language-shell
    $ cd aem-guides-wknd-graphql/server-to-server-app
    $ npm install
    
  3. 可以使用命令运行应用程序:

    code language-none
    $ node index.js <AEM_HOST> <OPTIONAL_SERVICE_CONFIG_FILE_PATH>
    

    例如,要在未经授权的情况下对AEM Publish运行应用程序,请执行以下操作:

    code language-shell
    $ node index.js https://publish-p123-e789.adobeaemcloud.com
    

    要以授权方式针对AEM Author运行应用程序,请执行以下操作:

    code language-shell
    $ node index.js https://author-p123-e456.adobeaemcloud.com ./service-config.json
    
  4. 终端应打印来自WKND引用站点的JSON冒险列表。

代码

以下概要介绍了如何构建服务器到服务器Node.js应用程序,它如何连接到AEM Headless以使用GraphQL持久查询检索内容,以及数据如何呈现。 完整代码可在上找到 GitHub.

服务器到服务器AEM Headless应用程序的常见用例是将AEM中的内容片段数据同步到其他系统,但此应用程序会刻意简化,并会从持久查询中打印JSON结果。

持久查询

遵循AEM Headless最佳实践,应用程序使用AEM GraphQL持久查询来查询冒险数据。 该应用程序使用两个持久查询:

  • wknd/adventures-all 持久查询,该查询返回在AEM中使用一组删节的属性进行的所有冒险。 此持久查询驱动初始视图的冒险列表。
# Retrieves a list of all Adventures
#
# Optional query variables:
# - { "offset": 10 }
# - { "limit": 5 }
# - {
#    "imageFormat": "JPG",
#    "imageWidth": 1600,
#    "imageQuality": 90
#   }
query ($offset: Int, $limit: Int, $sort: String, $imageFormat: AssetTransformFormat=JPG, $imageWidth: Int=1200, $imageQuality: Int=80) {
  adventureList(
    offset: $offset
    limit: $limit
    sort: $sort
    _assetTransform: {
      format: $imageFormat
      width: $imageWidth
      quality: $imageQuality
      preferWebp: true
  }) {
    items {
      _path
      slug
      title
      activity
      price
      tripLength
      primaryImage {
        ... on ImageRef {
          _path
          _dynamicUrl
        }
      }
    }
  }
}

创建AEM Headless客户端

const { AEMHeadless, getToken } = require('@adobe/aem-headless-client-nodejs');

async function run() {

    // Parse the AEM host, and optional service credentials from the command line arguments
    const args = process.argv.slice(2);
    const aemHost = args.length > 0 ? args[0] : null;                // Example: https://author-p123-e456.adobeaemcloud.com
    const serviceCredentialsFile = args.length > 1 ? args[1] : null; // Example: ./service-config.json

    // If service credentials are provided via command line argument,
    // use `getToken(..)` to exchange them with Adobe IMS for an AEM access token
    let accessToken;
    if (serviceCredentialsFile) {
        accessToken = (await getToken(serviceCredentialsFile)).accessToken;
    }

    // Instantiate withe AEM Headless client to query AEM GraphQL APIs
    // The endpoint is left blank since only persisted queries should be used to query AEM's GraphQL APIs
    const aemHeadlessClient = new AEMHeadless({
        serviceURL: aemHost,
        endpoint: '',           // Avoid non-persisted queries
        auth: accessToken       // accessToken only set if the 2nd command line parameter is set
    })
    ...
}

执行GraphQL持久查询

AEM持久查询通过HTTPGET执行,因此, 适用于Node.js的AEM Headless客户端 用于 执行持久的GraphQL查询 AEM并检索冒险内容。

通过调用调用持久查询 aemHeadlessClient.runPersistedQuery(...),并传递持久的GraphQL查询名称。 GraphQL返回数据后,将其传递到简化的 doSomethingWithDataFromAEM(..) 函数打印结果,但通常会将数据发送到另一个系统,或根据检索到的数据生成一些输出。

// index.js

async function run() {
    ...
    try {
        // Retrieve the data from AEM GraphQL APIs
        data = await aemHeadlessClient.runPersistedQuery('wknd-shared/adventures-all')

        // Do something with the data from AEM.
        // A common use case is sending the data to another system.
        await doSomethingWithDataFromAEM(data);
    } catch (e) {
        console.error(e.toJSON())
    }
}
recommendation-more-help
e25b6834-e87f-4ff3-ba56-4cd16cdfdec4