【问题标题】:Angular universal redirect 404 page not found找不到角度通用重定向 404 页面
【发布时间】:2020-11-29 06:37:53
【问题描述】:

我有一个 Angular Universal 项目,它使用 ssr 的 firebasecloud 连接到 firebase。 当我尝试转到路由中不存在的页面时,我收到 403 错误,它不会让我回到正确的页面。 我看到这个页面

这些是我的文件: server.ts:

export function app() {
    const server = express();
    const distFolder = join(process.cwd(), 'dist/ricicla-risparmia-rinnova/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'
    }));

    server.get('*', (req, res) => {
        res.render(indexHtml, {req, providers: [{provide: APP_BASE_HREF, useValue: req.baseUrl}]});
    });


    return server;
}

app-routing.module.ts:

const routes: Routes = [
    {path: '', redirectTo: 'home', pathMatch: 'full'},
    {path: 'home', component: HomeComponent},
    {path: 'login', canActivate: [AuthAdminGuard], component: LoginComponent},
    {path: 'articolo', loadChildren: () => import('./components/articolo/articolo.module').then(m => m.ArticoloModule)},
    {path: 'admin', canActivate: [AuthAdminGuard], loadChildren: () => import('./admin/admin.module').then(m => m.AdminModule)},
    {path: '404', component: NotFoundComponent},
    {path: '**', redirectTo: '404'}
];

【问题讨论】:

    标签: angular server-side-rendering angular-universal


    【解决方案1】:

    我在这里分享一个相同的工作示例,我希望这可以解决问题! check this

    如果我的示例代码不能解决您的问题,您的代码似乎是正确的,请与您面临的错误分享错误或工作示例代码。

    【讨论】:

    • 你的项目没有通用性,如果我在本地启动我的项目,重定向工作,一旦它与 ssr 一起工作,它就不再工作并给我那个错误页面
    • 你所说的“没有普遍性”是什么意思?如果您有 SSR 问题,那么您的服务器配置(Nginx/apache)可能会覆盖某些内容。因为如果 URL 与给定路径不匹配,它将回退到给出路径。
    • 我用firebase做托管,ssr一直在firebase的云功能,服务器配置我放在上面
    • 看看这个可能对你有帮助! link
    猜你喜欢
    • 2018-03-29
    • 2013-03-31
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-16
    • 2017-03-04
    • 1970-01-01
    相关资源
    最近更新 更多