首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >节点表示服务器不使用aws lambda呈现静态内容的角应用程序

节点表示服务器不使用aws lambda呈现静态内容的角应用程序
EN

Stack Overflow用户
提问于 2020-05-14 20:03:02
回答 1查看 1.4K关注 0票数 1

我最近尝试在AWS上部署角应用程序,但是我的静态内容得到了异常403。我使用express js来配置服务器。请访问和检查这个AWS lambda URL,我正面临这个问题。

这是server.ts文件

代码语言:javascript
复制
    import 'zone.js/dist/zone-node';
    import { join } from 'path';
    import * as express from 'express';
    import { AppServerModule } from './src/main.server';
    import { APP_BASE_HREF } from '@angular/common';
    import { ngExpressEngine } from '@nguniversal/express-engine';
    import { existsSync } from 'fs';

    // Express server
    export const app = express();

    const PORT = process.env.PORT || 4000;
    const DIST_FOLDER = join(process.cwd(), 'dist/myPleaks/browser');
    const indexHtml = existsSync(join(DIST_FOLDER, 'index.original.html')) ? 'index.original.html' : 'index';

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

    app.set('view engine', 'html');
    app.set('views', DIST_FOLDER);

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

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

这里是lambda.js

代码语言:javascript
复制
    const awsServerlessExpress = require('aws-serverless-express');
    const server = require('./dist/myPleaks/server/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',
        'font/ttf',
        'font/otf',
    ];

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

这里是serverless.yml

代码语言:javascript
复制
      service: mypleaks # Name whatever as you like!
  plugins:
    - serverless-apigw-binary
    - serverless-offline

  provider:
    name: aws
    runtime: nodejs10.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:
    contentCompression: 1024
    apigwBinary:
      types:
        - '*/*'

  functions:
    api:
      handler: lambda.handler
      events:
        - http: ANY /

提前谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-16 18:19:49

我认为你的问题与你的serverless.yml有关。这确实令人沮丧,因为我在无服务器的文档中找不到指向您的任何地方,但我认为您的问题是,您告诉Lambda只响应一条特定的路径,/

如果加载获得403分的资源直接在浏览器中,您将看到得到的是错误:

代码语言:javascript
复制
{"message":"Missing Authentication Token"}

在谷歌上搜索这条信息表明,这是一个特定的错误。

请查看我找到了这个例子,其中他们添加了另一个http事件,以便在所有路径上接受请求:

代码语言:javascript
复制
- http:
          path: /{any+} # this matches any path, the token 'any' doesn't mean anything special
          method: ANY
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61806437

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档