【发布时间】: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