Node JS重定向给了我一堆html文本

时间:2014-07-25 09:31:57

标签: javascript node.js redirect render

想问一个关于nodeJS重定向和渲染的问题。

app.js

在我的app.js中,我有这些代码,效果很好。

app.get('/dashboard', routes.dashboard);
app.get('/error/404', error.notFound);

然后,进一步研究

的方法

error.notFound

从URL

加载时也能正常工作
exports.illegal = function(req, res){
    var authLevel = '';
    var entityName = '';
    var entityId = '';
    var deviceId = '';

    if (req.session.authorizationLevel) authLevel = req.session.authorizationLevel;
    if (req.session.name) entityName = req.session.name;
    if (req.session.entityId) entityId = req.session.entityId;
    if (req.session.deviceId) deviceId = req.session.deviceId;

    res.render('error/illegal', {
        title: 'Illegal'
        , viewClass: 'illegal'
        , ngController : ''
        , entityName : entityName
        , entityId : entityId
        , deviceId : deviceId
        , authorizationLevel : authLevel
    });
};

routes.dashboard

从URL

加载时也能正常工作
exports.dashboard = function(req, res){
    //Method for authorization, see below. This is giving me error.
    authorizationHelper.authorizationLevels(req, res, 400);

    var authLevel = '';
    var entityName = '';
    var entityId = '';
    var deviceId = '';

    if (req.session.authorizationLevel) authLevel = req.session.authorizationLevel;
    if (req.session.name) entityName = req.session.name;
    if (req.session.entityId) entityId = req.session.entityId;
    if (req.session.deviceId) deviceId = req.session.deviceId;

    res.render('dashboard', {
        title: 'Dashboard'
        , viewClass: 'dashboard'
        , ngController: 'dashboardController'
        , entityName : entityName
        , entityId : entityId
        , deviceId : deviceId
        , authorizationLevel : authLevel
    });

};

这是给我一些错误的棘手部分。

    //Method for authorization, see below. This is giving me error.
    authorizationHelper.authorizationLevels(req, res, 400);

authorizationHelper

此方法将检查是否允许用户查看页面,否则将重定向

exports.authorizationLevels = function(req, res, levelRequired){
    if (req.session.authorizationLevel < levelRequired || !req.session.authorizationLevel){
        //user is not allowed to view the page, redirect to illegal page
        res.redirect('/error/401');
    }
}

问题是,从顶部到此res.redirect('/ error / 401')一切正常,有时在服务器上,它不会呈现页面,而是呈现一大堆HTML文字和一些标题!!

enter image description here

请帮忙!

1 个答案:

答案 0 :(得分:0)

你可以改变

exports.authorizationLevels = function(req, res, levelRequired) {
  if (req.session.authorizationLevel < levelRequired || !req.session.authorizationLevel){
      //user is not allowed to view the page, redirect to illegal page
      res.redirect('/error/401');
  }
}

exports.authorizationLevels = function(req, res, levelRequired) {
  if (req.session.authorizationLevel < levelRequired || !req.session.authorizationLevel){
      //user is not allowed to view the page, redirect to illegal page
      res.redirect('/error/401');
      return false;
  }
  return true;
}

authorizationHelper.authorizationLevels(req, res, 400);

if (!authorizationHelper.authorizationLevels(req, res, 400))
  return;

这样可以防止在未经授权的情况下呈现仪表板。

此外,您可以考虑集中处理错误,以便只安装错误处理程序中间件((err, req, res, next))并在那里进行错误模板渲染(同时只执行next(401);等错误处理路由处理程序),除非您希望他们在浏览器中看到/error/401和此类URL。