Redirect to UI-router state in Node.js

China☆狼群 提交于 2019-12-25 17:19:32

问题


I display list of the products; once clicked on the product row, the full screen detail view is displayed for it. I am using ui-router for front end routing to to get from list view to detail view. Example of the address url: http://localhost:3000/detail/product1 .

If I enter this url into browser address, it does not redirect to that product detail, instead it tries to load index.html since it is the default page in nodejs. How can I redirect to ui-router state in node.js?

Angular App.js

'use strict';

/* App Module */

var myApp = angular.module("myApp", ['ui.router']);

myApp.config(['$stateProvider', '$urlRouterProvider','$locationProvider',
           function($stateProvider, $urlRouterProvider,$locationProvider) {


$urlRouterProvider.otherwise('/');

$stateProvider

.state('/', {
    url: '/',
    templateUrl: 'partials/listView.html'
})

    .state('list', {
        url: '/',
        templateUrl: 'partials/listView.html'
    })
    .state('detail', {
        url: '/detail/:key',
        params: {
             key: { value: "" }
        },
        templateUrl: 'partials/detailView.html',
        controller: 'DetailController'
    })

// use the HTML5 History API
$locationProvider.html5Mode(true); 
}]);

nodeJS app.js:

    var express = require('express')
    , routes = require('./routes')
    , http = require('http')
    , path = require('path')
    , bodyParser = require("body-parser");

    var app = express();

    app.set('port', process.env.PORT || 80);

    //front end views are in /public folder; using html for views
    app.set('views', __dirname + '/public');
    app.engine('html', require('ejs').renderFile);
    app.set('view engine', 'html');

    app.use(bodyParser.json());
    app.use(bodyParser.json({ type: 'application/vnd.api+json' })); 
    //parse application/x-www-form-urlencoded
    app.use(bodyParser.urlencoded({ extended: true })); 


    //using index.html as starting point
    app.use(express.static(path.join(__dirname, '/public')));
    app.use(express.logger('dev'));

    app.use(express.methodOverride());
    app.use(app.router);


    //development only
    if ('development' == app.get('env')) {
       app.use(express.errorHandler());
    }

    app.get('/', routes.index); //redirect to index.html on page load
    app.get('/detail/*', function(req, res){

    var urlSegments = req.url.split('/',3);
    var key=urlSegments[2]; // product id 


    // ??? How can I redirect to detail state in angular ui-router here passing the key of the product ???
     });



app.get('*', function(req, res) {
    res.sendfile('./public/index.html'); // load the single view file (angular will handle the page changes on the front-end)
});

http.createServer(app).listen(app.get('port'), function() {
    console.log('Express server listening on port ' + app.get('port'));
});

Thank you


回答1:


The issue was fixed when state was changed to use query in url as:

.state('detail', {
   url: '/detail?key',
   params: {
     key: { value: "" }
   },
   templateUrl: 'partials/detailView.html',
   controller: 'DetailController'
})


来源:https://stackoverflow.com/questions/37266474/redirect-to-ui-router-state-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!