嗨,我在 Angular 的基础上使用 Ionic 4 做了一个页面,所以我使用带有守卫的 Angular 路由来管理访问。
我使用在 Expres JS 上制作的 APIRest 获取信息(我这样做了)。
然后我在互联网上发布了该站点,但是我在 Ionic 4 上有一些规则,如果用户使用该 url 刷新页面,Express JS 会尝试搜索,但是 Express 上不存在该规则,然后我收到错误 404
当我收到 404 错误时,我认为解决方案是重定向,但我不确定这是否是一个好习惯,我想使用最佳选项
const express = require('express'),
routes= require('./routes/router')
路线
router
.get('/', (req, res) => {
res.sendFile(`${reqPath}/views/index.html`) // Redirect to Ionic App
})
.post('/login', Controller.login)
.post('/CreateUser', Controller.CreateUser)
.post('/SendAlert', Controller.Alert)
.post('/Articles',Controller.Articles)
Ionic App app-routing.module.ts
const routes: Routes = [
{ path: '', redirectTo: 'login', pathMatch: 'full' },
{ path: 'login', loadChildren: './Login/login/login.module#LoginPageModule', canActivate: [LoginGuardService] },
{ path: 'home', loadChildren: './home/home/home.module#HomePageModule', canActivate: [AuthGuardService] },
{ path: 'add-user', loadChildren: './User/add-user/add-user.module#AddUserPageModule', canActivate: [AuthGuardService]},
{ path: 'user/:user', loadChildren: './User/user/user.module#UserPageModule', canActivate: [UserGuardService] },
{ path: 'add-article/:idarticle', loadChildren: './User/add-article/add-article.module#AddArticlePageModule', canActivate: [UserGuardService] },
{ path: 'add-device/:iddevice', loadChildren: './Device/add-device/add-device.module#AddDevicePageModule', canActivate: [DeviceGuardService] },
{ path: 'simulations/:device', loadChildren: './Device/simulations/simulations.module#SimulationsPageModule', canActivate: [DeviceGuardService] },
{ path: 'profile', loadChildren: './Profile/profile/profile.module#ProfilePageModule' },
];
我可以访问登录名,并且可以正常访问,但是如果我导航到http://mypage.com/user/FirstUser页面就可以了,直到刷新页面,我收到 404 错误,因为 Express 上不存在该规则JS
我不知道 Ionic 中的路由器是如何做的。但是如果它在 express js 中没有路由器,则可以使用这个。
router
.get('user/:user', (req, res) => {
res.sendFile(`response_file_to_redirect_Ionic`) // Redirect to Ionic App
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句