AngularJS和Ex preSS路由问题 [英] AngularJS and Express Routing issue

查看:185
本文介绍了AngularJS和Ex preSS路由问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AngularJS和Ex pressJS和具有路由的问题。我看到了许多其他职位,但没有这些解决方案似乎工作。这是我在Ex preSS路线:

  module.exports =功能(应用程序,验证){
    //阿比路线
    VAR = myController的需要('../应用程序/控制器/ myController的');
    app.get('/ API / dostuff /:身份证',mycontroller.getBlockByHash);    //首页路线
    app.get(/功能(REQ,RES){
      res.render(索引);
    });
  };

当我去我的根 / ,一切正常。防爆pressJS担任了我的索引和角度拿起休息。当我点击一个链接 /块,它按预期工作,因为AngularJS拿起路线。但是,当我刷新,我得到一个未找到404错误。

我试过 app.get('*'来代替,但是这给了我一个完全不同的错误的位置没有任何负载。

我用以创建具有防爆preSS的基本页面结构。我的前preSS的配置是:

  app.use(如press.favicon());
app.use(如press.static(config.root +'/公'));


解决方案

当使用html5Mode的的文档说:


  

使用该模式需要URL重写,服务器端的,基本上你不得不重写所有的链接到应用程序的入口点(例如,index.html)


什么它没有提到的是:


  • 您应该排除的静态资源像脚本/风格/图像/字体等。

  • 您也应该排除您的宁静的API

您的情况:

您得到了错误有服务到HTML脚本标记前preSS和浏览器不能把它解析为一个有效的JavaScript。

使用前press.static 来提供静态资产,然后使用 app.get('*',重定向所有其他请求您angular.js入口点(的index.html)。

前press.js中间件为了做计数!

I'm using AngularJS and ExpressJS and having an issue with routing. I saw many other posts but none of those solutions seemed to work. Here is my routes in Express:

module.exports = function(app, auth) {
    //Api routes
    var mycontroller = require('../app/controllers/mycontroller');
    app.get('/api/dostuff/:id', mycontroller.getBlockByHash);

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

When I go to my root /, everything works as expected. ExpressJS serves up my index and angular picks up the rest. When I click a link /blocks, it works as expected since AngularJS picks up the route. But when I refresh, I get a 404 not found error.

I tried app.get('*' instead, but that gives me a completely different error where nothing loads.

I'm using Jade to create the basic page structure with Express. My Express config is:

app.use(express.favicon());
app.use(express.static(config.root + '/public'));

解决方案

When using html5Mode the documentation says:

Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)

What it doesn't mention is:

  • You should exclude static assets like scripts/styles/images/fonts etc.
  • You should also exclude your Restful API.

Your case:

The error you got there is express serving html into script tags and the browser fails to parse them as a valid javascript.

Use express.static to serve static assets and then use app.get('*', for redirecting all other requests to your angular.js entry point (index.html).

express.js middleware order do counts!

这篇关于AngularJS和Ex preSS路由问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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