简体   繁体   English

“errorMessage”:在 AWS 无服务器上部署时“server.app.use 不是一个函数”

[英]"errorMessage": "server.app.use is not a function" while deploying on AWS serverless

Here I am trying to deploy my angular application on AWS serverless lambda. But it gets me this error.在这里,我试图在 AWS 无服务器 lambda 上部署我的 angular 应用程序。但它让我遇到了这个错误。 I ran the command serverless:deploy to deploy it.我运行命令serverless:deploy来部署它。 But the endpoint shows me error {"message": "Internal server error"} .但是端点向我显示错误{"message": "Internal server error"} what am I doing wrong?我究竟做错了什么? Am I missing any file or anything?我缺少任何文件或任何东西吗? Whyy does it show "server.app.use is not a function" .为什么它显示"server.app.use is not a function" Any help will be appreciated.任何帮助将不胜感激。 The error shown below is taken from lambda logs on AWS.下面显示的错误取自 AWS 上的 lambda 日志。 I followed this tutorial https://www.seijivillafranca.com/post/serverless-angular-with-aws-lambda我遵循了本教程https://www.seijivillafranca.com/post/serverless-angular-with-aws-lambda

error错误

> undefined ERROR   Uncaught Exception  {
    "errorType": "TypeError",
    "errorMessage": "server.app.use is not a function",
    "stack": [
        "TypeError: server.app.use is not a function",
        "    at Object.<anonymous> (/var/task/lambda.js:25:12)",
        "    at Module._compile (internal/modules/cjs/loader.js:1085:14)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)",
        "    at Module.load (internal/modules/cjs/loader.js:950:32)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:790:12)",
        "    at Module.require (internal/modules/cjs/loader.js:974:19)",
        "    at require (internal/modules/cjs/helpers.js:93:18)",
        "    at _tryRequireFile (/var/runtime/UserFunction.js:63:32)",
        "    at _tryRequire (/var/runtime/UserFunction.js:151:20)",
        "    at _loadUserApp (/var/runtime/UserFunction.js:197:12)"
    ]
}

serverless.yml无服务器.yml

> service: project
plugins:
  - serverless-apigw-binary
  - serverless-offline
provider:
  name: aws
  runtime: nodejs14.x
  memorySize: 192
  timeout: 10
  stage: production
  region: ap-south-1

package:
  exclude:
   - src/**
   - node_modules/**
   - firebug-lite/**
   - e2e/**
   - coverage/**
   - '!node_modules/aws-serverless-express/**'
   - '!node_modules/binary-case/**'
   - '!node_modules/type-is/**'
   - '!node_modules/media-typer/**'
   - '!node_modules/mime-types/**'
   - '!node_modules/mime-db/**'

custom:
  apigwBinary:
    types:
      - '*/*'

functions:
  api:
    handler: lambda.handler
    events:
      - http: ANY {proxy+}
      - http: ANY /

lambda.js lambda.js

> const awsServerlessExpress = require("aws-serverless-express");
const server = require("./dist/project/serverless/main");
const awsServerlessExpressMiddleware = require("aws-serverless-express/middleware");

const binaryMimeTypes = [
  "application/javascript",
  "application/json",
  "application/octet-stream",
  "application/xml",
  "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",
  "application/x-font-ttf",
];

server.app.use(awsServerlessExpressMiddleware.eventContext());
const serverProxy = awsServerlessExpress.createServer(
  server.app,
  null,
  binaryMimeTypes
);
module.exports.handler = (event, context) =>
  awsServerlessExpress.proxy(serverProxy, event, context);
 

serverless.ts无服务器.ts

import 'zone.js/dist/zone-node';

import { ngExpressEngine } from '@nguniversal/express-engine';
import * as express from 'express';
import { join } from 'path';

import { AppServerModule } from './src/main.server';
import { APP_BASE_HREF } from '@angular/common';
import { existsSync } from 'fs';

// The Express app is exported so that it can be used by serverless Functions.
export function app(): express.Express {
  const server = express();
  const distFolder = join(process.cwd(), 'dist/project/browser');
  const indexHtml = existsSync(join(distFolder, 'index.original.html')) ? 'index.original.html' : 'index';

  // Our Universal express-engine (found @ https://github.com/angular/universal/tree/master/modules/express-engine)
  server.engine('html', ngExpressEngine({
    bootstrap: AppServerModule,
  }));

  server.set('view engine', 'html');
  server.set('views', distFolder);

  // Example Express Rest API endpoints
  // server.get('/api/**', (req, res) => { });
  // Serve static files from /browser
  server.get('*.*', express.static(distFolder, {
    maxAge: '1y'
  }));

  // All regular routes use the Universal engine
  server.get('*', (req, res) => {
    res.render(indexHtml, { req, providers: [{ provide: APP_BASE_HREF, useValue: req.baseUrl }] });
  });

  return server;
}



// Webpack will replace 'require' with '__webpack_require__'
// '__non_webpack_require__' is a proxy to Node 'require'
// The below code is to ensure that the server is run only when not requiring the bundle.


export * from './src/main.server';

package.json package.json

{
  "name": "project",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "dev:ssr": "ng run project:serve-ssr",
    "serve:ssr": "node dist/project/server/main.js",
    "build:ssr": "ng build --prod && ng run project:server:production",
    "prerender": "ng run project:prerender",
    "serve:serverless": "serverless offline start",
    "build:serverless": "ng build --prod && ng run project:serverless:production",
    "serverless:deploy": "npm run serverless:deploy"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.0.6",
    "@angular/common": "~10.0.6",
    "@angular/compiler": "~10.0.6",
    "@angular/core": "~10.0.6",
    "@angular/forms": "~10.0.6",
    "@angular/platform-browser": "~10.0.6",
    "@angular/platform-browser-dynamic": "~10.0.6",
    "@angular/platform-server": "~10.0.6",
    "@angular/router": "~10.0.6",
    "@nguniversal/express-engine": "^10.0.2",
    "@vendia/serverless-express": "^4.5.3",
    "aws-serverless-express": "^3.4.0",
    "express": "^4.15.2",
    "rxjs": "~6.5.5",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.5",
    "@angular/cli": "~10.0.5",
    "@angular/compiler-cli": "~10.0.6",
    "@nguniversal/builders": "^10.0.2",
    "@types/express": "^4.17.0",
    "@types/jasmine": "~3.5.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "serverless": "^2.18.0",
    "serverless-apigw-binary": "^0.4.4",
    "serverless-offline": "^8.4.0",
    "ts-node": "~8.3.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.5"
  }

} }

enter code here

Just ran into this error myself, it was due to exporting app() from server.ts as a function我自己刚遇到这个错误,这是由于从server.ts导出app()作为 function

// server.ts

export function app(): express.Express {
    ...
    return server;
}

and using it in lambda.js as an object instead.并在lambda.js其用作 object。

// lambda.js

const server = require('./dist/server/main');

...

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

I fixed it by simply changing these lines to:我通过简单地将这些行更改为来修复它:

// lambda.js

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在部署 Python Bottle 服务器时通过 AWS App Runner 的健康检查测试? - How to pass health check tests on AWS App Runner while deploying Python Bottle server? 使用无服务器部署 express 时出现内部服务器错误 - internal server error with when deploying express with serverless AWS Lambda function on Java with Serverless framework and GraalVM - AWS Lambda function on Java with Serverless framework and GraalVM 使用无服务器框架将 Python package 部署到 AWS lambda 时出错 - Error deploying Python package to AWS lambda using Serverless framework 如何在 ubuntu 服务器上部署 vuejs 应用程序时解决剪贴板错误 - How to resolve clipboard error while deploying vuejs app on ubuntu server Lambda 从 Serverless 部署 CloudFormation 时未继承权限 - Lambda not inheriting permissions while deploying CloudFormation from Serverless 如何在 AWS 无服务器 Function 上设置 Oauth 范围? - How to setup Oauth scopes at AWS Serverless Function? 如何将无服务器客户端与给定的 AWS 连接 Lambda function - how to connect a serverless client with a given AWS Lambda function 无服务器框架从单独的 function 获取 AWS“函数 url” - Serverless framework get AWS "function url" from separate function AWS Lambda 错误 - “errorType”:“string”,“errorMessage”:“handled” - AWS Lambda Error - "errorType": "string", "errorMessage": "handled"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM