简体   繁体   English

启用html5mode的Angular和Express路由

[英]Angular and Express routing with html5mode enabled

So my question is: how could I make angular and express routing work together with html5mode enabled? 所以我的问题是:如何在启用html5mode的情况下使角度路由和表达路由一起工作? When I change state from '/' to my admins state I get list of all admins and everything is fine until I refresh my page. 当我将状态从“ /”更改为管理员状态时,我会得到所有管理员的列表,并且一切都很好,直到刷新页面为止。 Then I get only json result of admins but my view disappears. 然后我只得到管理员的json结果,但是我的视图消失了。 I copied only peace of code which is relevant - to make it shorter, there is no syntax errors. 我只复制了与之相关的和平代码-为了简化代码,没有语法错误。

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

My file structure: 我的文件结构:

+-- client
|   app
|       admin.config.js
|       admin.js
|       admin.module.js
|   assets
|   views
|       admin
|           admin-edit.ejs
|           admin-list.ejs
|   lib
|       bower components
+-- server
|   routes
|       index.js
|       admins.js
|   models
|       Admin.js
|   config
|       dbconfig.js
|   server.js

In my Angular module.config I have: 在我的Angular module.config中,我有:

function config($stateProvider, $urlRouterProvider, $locationProvider) {
    $stateProvider
        .state('admins', {
            url        : '/admins',
            templateUrl: '/views/admin/admin-list.ejs',
            controller : 'AdminController',
            resolve    : {
                loginRequired: loginRequired,
                getAdminList : getAdminList
            }
        });

    $locationProvider.html5Mode(true);
    $urlRouterProvider.otherwise('/admins');

My server.js file looks like: 我的server.js文件如下所示:

/* Routes */
var routes = require('./routes/index');
var auth   = require('./routes/auth');
var users  = require('./routes/users');
var admins = require('./routes/admins');


var app = express();


/*************************************
 App initialization
 **************************************/

/* Auto increment mongoose plugin */
autoIncrement.initialize(connection);

/* Auth secret code*/
app.set('superSecret', dbconfig.SECRET);


/* View engine setup */
app.set('views', path.join(__dirname, '../client/views'));
app.set('view engine', 'ejs');


// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'client', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use(express.static('client'));

app.use('/', routes);
app.use('/auth', auth);
app.use('/users', users);
app.use('/admins', admins);

My index route looks like: 我的索引路径如下:

var express = require('express');
var router  = express.Router();

/* GET home page. */
router.get('/', function (req, res) {
    res.render('index');
});


module.exports = router;

I tried adding this into my server.js 我尝试将其添加到我的server.js

app.get('*', function (req, res){
        res.sendFile(path.join(__dirname + '/client/index.ejs'));
});

and in this case when I refresh page while my url is /admins I get downloaded index page. 在这种情况下,当我的网址是/ admins时刷新页面时,我会下载索引页面。

I also tried adding in my index.js route 我也尝试添加index.js路由

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

and as result of adding this is endless loop in my admins list. 添加此操作的结果是我的管理员列表中无休止的循环。

In my index.ejs I have 在我的index.ejs中,

<meta charset="utf-8">
<base href="/">

That's a lot of code. 那是很多代码。 I want to give you this . 我想给你这个 It is an answer I posted yesterday that I am proud of. 这是我昨天感到骄傲的答案。 It is a simple example of Angular routing in Angular 1.5. 这是Angular 1.5中Angular路由的简单示例。 You will have to add your line about $locationProvider.html5Mode(true); 您将必须添加有关$locationProvider.html5Mode(true); because I am not using it. 因为我没有使用它。

After some time I found solution here , instead of specific pages I added asterisk (*) to every get request I made so now my auth route looks like: 一段时间后,我在这里找到解决方案,而不是在特定页面上添加星号(*)到我发出的每个获取请求,因此现在我的身份验证路由如下所示:

router.route('/signup')

    /* Render index by default */
    .get(function (req, res) {
        res.render('index', {
            page: req.params.page
        });

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM