Html5Mode - 刷新angularjs页面问题

时间:2015-03-30 10:06:52

标签: angularjs node.js cordova angular-ui-router cordova-cli

我正在开发一个angularJS应用,它的格式(在开发中) -

http://localhost:6001/#/home/index
http://localhost:6001/#/content/index

由于此应用程序也可以在桌面浏览器上使用,我希望网址没有“#”,所以我在我的应用程序的配置部分添加了这个 -

$locationProvider.html5Mode(true);

哪个工作正常,网址不显示'#'。但现在有一个问题。在浏览器中刷新特定页面时,现在我收到此错误 -

Cannot GET /home/index

是否需要进行其他更改才能使其正常工作?

注意:此应用使用cordova CLI并托管​​在Node.js服务器上。

2 个答案:

答案 0 :(得分:1)

尝试将您的路线放入:

app.get(*, function(req,res) {
  //return the main index file
});

这将导致任何不匹配的路线为索引页面提供服务,并从那里开始处理这条路线。

答案 1 :(得分:0)

试试这个:

  1. 确保server.js文件中包含以下这些行

    <FrameLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Overlayed Text"
            android:layout_gravity="center"/>
    
    </FrameLayout>
    

    为什么这样?请在此处阅读 - https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#how-to-configure-your-server-to-work-with-html5mode

  2. 第一步导致我的应用程序出现在以下console.log消息中:

    var express = require('express');
    var app = express();
    app.use(express.static(__dirname + '/public.build'));
    app.use(function(req, res) {
    res.sendFile('index.html', { root: __dirname + "/public.build" });
    });
    

    正如您所看到的,样式正在尝试从不存在的路径加载(/ board /文件夹不在应用程序中,它是ui-router中的状态之一)。 为了解决这个问题,我更换了以下一行

    Resource interpreted as Stylesheet but transferred with MIME type
    text/html: "http://localhost:8080/board/css/style.css". 
    

    这一个:

    <link rel="stylesheet" href="css/style.css">
    
相关问题