Node.js 路由无法正常工作
我已经创建了一个angular 2应用程序。这是app.routes.ts的Node.js 路由无法正常工作,node.js,angular,typescript,angular-routing,Node.js,Angular,Typescript,Angular Routing,我已经创建了一个angular 2应用程序。这是app.routes.ts的 import { Routes } from '@angular/router'; import { RoleListComponent } from './role-list.component'; import { RoleEditComponent } from './role-edit.component'; export const appRoutes: Routes = [ { p
import { Routes } from '@angular/router';
import { RoleListComponent } from './role-list.component';
import { RoleEditComponent } from './role-edit.component';
export const appRoutes: Routes = [
{
path: 'role-list', component: RoleListComponent
}
, {
path: 'role-edit', component: RoleEditComponent
}
, {
path: '', redirectTo: '/role-list', pathMatch: 'full'
}
];
屏幕上有两个按钮可导航到该页面。
然后我在bs config.js
bs config.js
var proxy = require('http-proxy-middleware');
var apiProxy = proxy('/services', {
target: 'http://localhost:9000',
changeOrigin: true
});
module.exports = {
port: 9001,
server: {
baseDir: "src",
routes: {
"/node_modules": "node_modules"
},
middleware: {
1: apiProxy,
}
}
};
现在,当我启动应用程序时,它会点击http://localhost:9001/role-列出并在屏幕上显示错误消息
Cannot GET /role-list
如果我点击localhost:9001
,它就会工作并显示http://localhost:9001/role-在地址栏中列出
URL,但如果我直接点击相同的URL,则会显示上述错误
现在,如果我删除代理中间件,它可以正常工作。我不明白是什么问题?我配置有问题吗?可能的问题是什么?您的问题在服务器端
您需要将每个角度路由(在服务器端)路由到index.html
中指定的基本路径。例如,在nodeJS
中就是这样做的:
app.get('/*', (req, res) => {
res.render('index', {req, res});
});
或者Apache
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
重新启动发动机
重写基/
重写规则^index\.html$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.html[L]
否则,当您的客户到达例如www.example.com/path/someThing
时,您的服务器将在/var/www/example.com/path/someThing/
中查找index.html
文件,而不是/var/www/example.com/
,您的问题在服务器端
您需要将每个角度路由(在服务器端)路由到index.html
中指定的基本路径。例如,在nodeJS
中就是这样做的:
app.get('/*', (req, res) => {
res.render('index', {req, res});
});
或者Apache
:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
重新启动发动机
重写基/
重写规则^index\.html$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.html[L]
否则,当您的客户到达例如www.example.com/path/someThing
时,您的服务器将在/var/www/example.com/path/someThing/
中查找index.html
文件,而不是/var/www/example.com/
,这可能是因为服务器(代理)不支持HTML5状态。您可以尝试将useHash:true
添加到您的路由器配置中(您将获得不同的URL,但如果它以这种方式工作,将验证我的假设),谢谢!!这是工作,但哈希显示在我的网址,我不想这样。有没有其他解决办法?正如我在之前的评论中提到的,这只是为了验证。您需要一个支持HTML5 pushState的服务器,或者配置您必须支持它的服务器。我不知道你的服务器,但是对于所有类型的服务器都有很多答案。整个配置过程都是关于服务器在客户端请求未知资源时返回index.html
,这可能是因为服务器(代理)不支持HTML5 pushState。您可以尝试将useHash:true
添加到您的路由器配置中(您将获得不同的URL,但如果它以这种方式工作,将验证我的假设),谢谢!!这是工作,但哈希显示在我的网址,我不想这样。有没有其他解决办法?正如我在之前的评论中提到的,这只是为了验证。您需要一个支持HTML5 pushState的服务器,或者配置您必须支持它的服务器。我不知道您的服务器,但是对于所有类型的服务器都有很多答案。整个配置过程都是关于服务器在客户端请求未知资源时返回index.html