gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 22:37:20 24 4
gpt4 key购买 nike

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

app.js

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

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

然后,进一步探讨方法

错误.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);

授权助手

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

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

请帮忙!

最佳答案

你可以改变

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。

关于javascript - Node JS 重定向给了我一堆 html 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24952467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com