我新尝试在AWS Lambda上部署角度应用程序,但我的静态内容获取异常403。我正在使用Express js配置服务器。请访问并检查此[AWS lambda URL] https://www.mypleaks.com,以解决此问题。
这是server.ts文件
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
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
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 /
提前感谢。
我认为您的问题与您的serverless.yml
有关。这真的很令人沮丧,因为在无服务器文档中找不到任何指向您的地方,但是我认为您的问题是您告诉Lambda仅响应一种特定的路由/
。
如果加载获得403的资源directly in the browser,则会看到错误:
{"message":"Missing Authentication Token"}
搜索该消息表明它是特定于lambda的错误。
[在this example I found中添加另一个http事件以在所有路径上接受请求的地方:
- http:
path: /{any+} # this matches any path, the token 'any' doesn't mean anything special
method: ANY