AngularJS路由防爆pressJS [英] AngularJS routing in ExpressJS

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

问题描述

我有防爆pressJS一个服务器的NodeJS上的AngularJS应用。现在,我的服务应用角度为静态文件:

I have an AngularJS app on a NodeJS server with ExpressJS. Now I am serving an Angular app as static files:

app.use(express.static(__dirname + '/app'));

但在导航,我有一个#符号:

But in navigation, I have a # sign:

http://127.0.0.1:8080/#/queryes

要解决这个问题,我把这个code在角路由器:

To solve this problem, I put this code in the Angular Router:

$locationProvider.html5Mode(true);

但现在我无法得到部分景色。我如何用防爆pressJS混合角路线?

But now I can't get partial views. How do I mix Angular Routes with ExpressJS?

推荐答案

为了使用AngularJS html5mode随着防爆preSS,你必须服务的index.html所有请求离开所有的路由到AngularJS。我有同样的问题而回。

In order to use AngularJS html5mode along with Express, you must serve "index.html" for all requests to leave all routing up to AngularJS. I had this same problem a while back.

因此​​,首先声明的所有API端点的路线,任何静态文件目录(CSS,JS,谐音等),再服index.html的所有剩余请​​求。例如:

So first, you declare all API endpoint routes, any static file directories (CSS, JS, partials, etc), and then serve index.html for all remaining requests. For example:

    // serve all asset files from necessary directories
    app.use("/js", express.static(__dirname + "/app/js"));
    app.use("/img", express.static(__dirname + "/app/img"));
    app.use("/css", express.static(__dirname + "/app/css"));
    app.use("/partials", express.static(__dirname + "/app/partials"));
    app.use("/templates", express.static(__dirname + "/app/templates"));

    // any API endpoints
    app.post('/api/v1/auth/login', routes.auth.login);

    // serve index.html for all remaining routes, in order to leave routing up to angular
    app.all("/*", function(req, res, next) {
        res.sendfile("index.html", { root: __dirname + "/app" });
    });

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

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