我已经设法让它在带有 Angular 9 的 Loopback 4 中工作。在 Angular 应用程序之后,文件夹ng build
的内容dist/\<app-name\>
应该放在 loopbackpublic
文件夹中。
然后,为了让 angular 路由器正常工作,所有 HTML 错误(特别是 404 Not found 错误)都应该重定向到 angular index.html
。
为此,我们必须绑定RestBindings.SequenceActions.REJECT
到自定义提供程序。只application.ts
包含新的绑定
this.bind(RestBindings.SequenceActions.REJECT).toInjectable(AngularRejectProvider);
AngularRejectProvider
可以从以下默认拒绝提供程序中获取@loopback/rest/src/providers/reject.provider.ts
:
// Copyright IBM Corp. 2018,2020. All Rights Reserved.
// Node module: @loopback/rest
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import {BindingScope, inject, injectable} from '@loopback/core';
import {HttpError} from 'http-errors';
import {ErrorWriterOptions, writeErrorToResponse} from 'strong-error-handler';
import {RestBindings} from '@loopback/rest';
import {HandlerContext, LogError, Reject} from '@loopback/rest';
// @ts-ignore
var accepts = require('accepts')
import path from 'path';
// TODO(bajtos) Make this mapping configurable at RestServer level,
// allow apps and extensions to contribute additional mappings.
const codeToStatusCodeMap: {[key: string]: number} = {
ENTITY_NOT_FOUND: 404,
};
@injectable({scope: BindingScope.SINGLETON})
export class AngularRejectProvider {
static value(
@inject(RestBindings.SequenceActions.LOG_ERROR)
logError: LogError,
@inject(RestBindings.ERROR_WRITER_OPTIONS, {optional: true})
errorWriterOptions?: ErrorWriterOptions,
): Reject {
const reject: Reject = ({request, response}: HandlerContext, error) => {
// ADD THIS
const resolvedContentType = accepts(request).types([
'text/html', 'html',
]);
switch(resolvedContentType) {
case 'text/html':
case 'html':
return response.sendFile(path.join(__dirname, '../../public/index.html'));;
}
// END ADD THIS
const err = <HttpError>error;
if (!err.status && !err.statusCode && err.code) {
const customStatus = codeToStatusCodeMap[err.code];
if (customStatus) {
err.statusCode = customStatus;
}
}
const statusCode = err.statusCode || err.status || 500;
writeErrorToResponse(err, request, response, errorWriterOptions);
logError(error, statusCode, request);
};
return reject;
}
}
新部分如下,它基本上将所有 HTML 错误重定向到 Angular:
const resolvedContentType = accepts(request).types([
'text/html', 'html',
]);
switch(resolvedContentType) {
case 'text/html':
case 'html':
return response.sendFile(path.join(__dirname, '../../public/index.html'));;
}