Saturday, 20 May 2017

Should express router.get intervene page loading?

I have built a mean-stack application. I use http to communicate data between front-end and back-end. For example,

In front-end, I have

app.factory('ops', ['$http', function ($http) {
    var o = {};
    o.get = function (id) {
        return $http.get('/ops/' + id)
            .then(function (res) {
                return res.data
            })
    };
    return o;
}]);

In the back end, routes/index.js, I have

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

... ...
router.get('/ops/:op', function (req, res, next) {
    console.log("/ops/:op");
    res.json(req.op);
})

module.exports = router;

And it is the job of ui-router to load a page like https://localhost:3000/ops/59202d6a38b09685ff6b0581:

.state('ops', {
    url: '/ops/{id}',
    ...

However, since I turned html5Mode to true, it seems that router.get('/ops/:op', function (req, res, next) { console.log("/ops/:op"); res.json(req.op); }) can directly load the page https://localhost:3000/ops/59202d6a38b09685ff6b0581. If I comment .state('ops' ... and load the page, I see /ops/:op is displayed in the console, and the content of the json data is shown in the browser.

Could anyone tell me if we should let router.get('/ops/:op' ... intervenue the loading of a page in this way? If not, how could we disable it?

PS: routers/index.js is referenced in app.js as follows:

var index = require('./routes/index');
app.use('/', index);



via SoftTimur

No comments:

Post a Comment