Node.js的追赶所有重定向路径始终呈现索引页角不管网址 [英] Node.js catch-all route with redirect always renders index page with Angular regardless of url

查看:108
本文介绍了Node.js的追赶所有重定向路径始终呈现索引页角不管网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从本质上讲,当我使用一个包罗万象的路线,并使用 res.redirect('/')不管我进入它总是呈现指数/主页的URL的(即角似乎并不看到完整的URL),但是如果我把 res.render('指数')在包罗万象的路线一切工作正常。我不想重复code和重定向到'/'应该工作,我可能已经犯了一个愚蠢的错误这里的某个地方和任何帮助将非常AP preciated!

角路由:

 的app.config(函数($ routeProvider,$ locationProvider){
$ locationProvider.html5Mode(真);$ routeProvider
    。什么时候('/',
        {
            templateUrl:'谐音/ home.jade
        })
    。当('/约,
        {
            templateUrl:'谐音/ about.jade
        })
    不然的话({redirectTo:'/'});
});

键入网​​站地址/约时,这将会正确地呈现约页

  app.get('/',函数(REQ,RES){
    res.render(索引);
});app.get('/谐音/:名称',函数(REQ,RES){
    res.render('谐音/+ req.params.name);
});app.get('*',函数(REQ,RES){
    res.render(索引);
});

这将始终只显示索引页面

  app.get('/',函数(REQ,RES){
    res.render(索引);
});app.get('/谐音/:名称',函数(REQ,RES){
    res.render('谐音/+ req.params.name);
});app.get('*',函数(REQ,RES){
    res.redirect('/');
});

配置是否有帮助:

  //配置
app.configure(函数(){
    app.set('口',process.env.PORT || 1337);
    app.set('意见',__dirname +'/意见');
    app.set(视图引擎','玉');
    app.use(如press.favicon());
    app.use(如press.logger('开发'));
    app.use(如press.bodyParser());
    app.use(如press.methodOverride());
    app.use(如press.static(path.join(__目录名称,'公共')));
    app.use(app.router);
});


解决方案

这是由设计。

当您使用 res.redirect('/'),节点/ EX preSS发送HTTP重定向,这将改变URL在浏览器中,因此,当你的索引模板被渲染,并运行角度code时,URL是 / ,不管用户输入的内容(重定向整点)。

当你忽略重定向并只发送模板作为响应,用的NodeJS一个HTTP 200(成功)的HTML内容一起响应。由于URL没有改变,当你的应用程序运行,角路由正确路线。

编辑补充:地址注释

而不是有两个途径呈现相同的模板,我会摆脱 / 路径的所有在一起,只是有包罗万象的渲染指数模板,然后将转向控制权交给角路由器。

否则,我会考虑在概念上区分你的路线:您所有的应用程序路径是专门发送到角路由器,你通过的NodeJS渲染静态路由,并使用您捕获所有呈现一个更合适的页面丢失或未知的资源(为您的用户更加有用)。

您可以使用正则表达式类语言指定一个处理程序:

  app.get('/()|(约)|(接触)/'功能(REQ,RES){/ * *手柄/});

Essentially when I use a catch-all route and use res.redirect('/') regardless of the url I enter it will always render the index/home page (ie Angular does not seem to 'see' the full url) however if I place res.render('index') in the catch-all route everything works fine. I don't want repeat code and redirecting to '/' should work, I have probably made a stupid mistake somewhere here and any help would be much appreciated!

Angular routing:

app.config(function ($routeProvider, $locationProvider) {
$locationProvider.html5Mode(true);

$routeProvider
    .when('/', 
        {   
            templateUrl: 'partials/home.jade'
        })
    .when('/about', 
        {
            templateUrl: 'partials/about.jade'
        })
    .otherwise( {redirectTo: '/'});
});

This will correctly render the about page when entering site-address/about:

app.get('/', function (req, res) {
    res.render('index');
});

app.get('/partials/:name', function (req, res) {
    res.render('partials/' + req.params.name);
});

app.get('*', function (req, res) {
    res.render('index');
});

This will always just show the index page:

app.get('/', function (req, res) {
    res.render('index');
});

app.get('/partials/:name', function (req, res) {
    res.render('partials/' + req.params.name);
});

app.get('*', function (req, res) {
    res.redirect('/');
});

Configuration if it helps:

// Configuration
app.configure(function () {
    app.set('port', process.env.PORT || 1337);
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.static(path.join(__dirname, 'public')));
    app.use(app.router);
});

解决方案

This is by design.

When you use res.redirect('/'), Node/Express is sending an HTTP redirect, which will change the URL in the browser, thus when your index template is rendered, and when the angular code is run, the URL is /, regardless of what the user entered (the whole point of the redirect).

When you omit the redirect and just send the template as a response, NodeJs responds with an HTTP 200 (success) along with HTML content. Since the URL didn't change, when your application runs, the angular routing properly routes.

EDIT TO ADD: Address Comment

Rather than have two routes render the same template, I would get rid of the / route all together, and just have the catch-all render the index template, which will then turn control over to the Angular router.

Otherwise, I would consider splitting your routes conceptually: All your application routes are specifically sent to angular router, and you render static routes via nodejs, and use your catch all to render a more appropriate page for a missing or unknown resource (more helpful for your users).

You can use regex-like languages to specify a single handler:

app.get('/()|(about)|(contact)/',function(req,res) {/* handle */});

这篇关于Node.js的追赶所有重定向路径始终呈现索引页角不管网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆