【问题标题】:Angular Universal with AWS Lambda - TypeError带有 AWS Lambda 的 Angular Universal - 类型错误
【发布时间】:2020-02-07 14:07:54
【问题描述】:

我目前正在努力获得一个使用 AWS Lambda 运行 SSR 的 Angular Universal 应用程序。

我安装了一个新的 Angular 应用程序

ng new my-angular-ssr-app

我将 Angular Universal 添加到我的 Angular 应用中

ng add @nguniversal/express-engine --clientProject my-angular-ssr-app

我测试了我的应用可以与 npm 一起使用,并使用 Postman 发出 GET-Request (http://localhost:4000/)

npm run build:ssr && npm run serve:ssr

我使用所需的依赖项 (aws-serverless-express) 压缩了以下文件夹和文件

- dist
- node_modules
- lambda.js

使用 AWS API Gateway 触发器将 ZIP 上传到新创建的 AWS Lambda 函数

向 AWS API Gateway URL 发出 GET 请求并获得:

{
    "message": "Internal server error"
}

Cloudwatch 为 GET 请求记录以下内容:

2019-10-10T07:17:17.935Z undefined ERROR Uncaught Exception
{
    "errorType": "TypeError",
    "errorMessage": "Cannot read property 'use' of undefined",
    "stack": [
        "TypeError: Cannot read property 'use' of undefined",
        "    at Object.<anonymous> (/var/task/index.js:25:12)",
        "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
        "    at Module.load (internal/modules/cjs/loader.js:653:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
        "    at Module.require (internal/modules/cjs/loader.js:692:17)",
        "    at require (internal/modules/cjs/helpers.js:25:18)",
        "    at _tryRequire (/var/runtime/UserFunction.js:75:12)",
        "    at _loadUserApp (/var/runtime/UserFunction.js:95:12)"
    ]
}
2019-10-10T07:17:17.963Z undefined INFO Node Express server listening on http://localhost:4000

我的 Lambda 函数如下所示:

const awsServerlessExpress = require('aws-serverless-express');
const server = require('./dist/server');
const awsServerlessExpressMiddleware = require('aws-serverless-express/middleware');

const binaryMimeTypes = [
  'application/javascript',
  'application/json',
  'application/octet-stream',
  'application/xml',
  'application/x-font-ttf',
  'image/jpeg',
  'image/png',
  'image/gif',
  'text/comma-separated-values',
  'text/css',
  'text/html',
  'text/javascript',
  'text/plain',
  'text/text',
  'text/xml',
  'image/x-icon',
  'image/svg+xml'
];

server.app.use(awsServerlessExpressMiddleware.eventContext());

const serverProxy = awsServerlessExpress.createServer(server.app, null, binaryMimeTypes);

module.exports.handler = (event, context) => awsServerlessExpress.proxy(serverProxy, event, context);

我显然做错了什么,但我不确定我错过了什么。有人知道我必须改变什么才能让它工作吗?

【问题讨论】:

    标签: angular amazon-web-services aws-lambda angular-universal


    【解决方案1】:

    在您的 server.ts 中,确保导出创建的 express 应用程序。如果没有该导出,lambda 函数就无法从 ./dist/server 访问该信息。

    更改应该像在const app = express() 前面扔export 一样简单。确保在再次尝试之前重建 Angular 应用程序。

    【讨论】:

      猜你喜欢
      • 2019-08-15
      • 2017-10-05
      • 1970-01-01
      • 2018-04-12
      • 2018-04-21
      • 2015-09-29
      • 2017-03-10
      • 2020-01-05
      • 2016-08-08
      相关资源
      最近更新 更多