javascript - Node JS redirects give me a bunch of html text -


Ask questions about nodes and redirects, and maybe rendering.

app.js

In my app js, I have these codes that work well.

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

Then,

error.notFound Increase

  export.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 / invalid', {title: 'illegal', viewClass: 'illegal', ngController: '', entity name: entity name, entity id: entity id, device id: device id, authorization: level : AuthLevel}); };   

Routes Dashboard

You also work while loading it from the URL

  exports .dashboard = Function (req, res) {// method for authentication, see below. This is giving me the error Authorization Helper.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', visual class: 'dashboard', ng controller: 'dashboard controller', entity name: entity name, entity id: entity id, device id: device id, authorization: Level: authLevel}); };   

Here's the hard part that gives me some error.

  Method for authorization, see below it is giving me the error Authorization Helper.AuthorizationLevels (req, res, 400);   

Authorization Heller

This method will check if the user is allowed to view the page or else it will redirect. >

  exports.authorizationLevels = function (req, res, levelRequired) {if (req.session.authorizationLevel & lt; levelRequired ||! Req.session.authorizationLevel) {// user is not allowed to view page Is an invalid redirect page, res.redirect ('/ error / 401'); }}   

The problem is, everything works properly on this res.redirect ('/ error / 401') and sometimes, on the server, this page But instead, this will provide a full bunch of HTML text and some headers! !

Enter image details here

Please help!

You

  export.authorizationLevels = function (req, res, LevelRequired) {If (req.session.authorizationLevel & lt; levelRequired ||! Req.session.authorizationLevel) {// user is not allowed to view page, invalid page res.redirect ('/ error / 401 '); }}   

to

  export.authorizationLevels = function (req, res, levelRequired) {if (req.session.authorizationLevel & lt; levelRequired || ! Req.session.authorizationLevel) {// User does not have permission to view page, Restricted to illegal page. Rdirect ('/ error / 401'); return false; } Back true; }   

and

  authorization. Helper.authorizationLevels (req, res, 400);     
  if (authorization. Assistant authorization level (request, ridge, 400)) return;   

This will prevent rendering your dashboard in case of unauthorized access.

In addition, you may want to consider centralizing your error handling so that you have an error handler middleware ( (err, req, res, next) ) and there Rendering error templates (whereas just forward (401); and errors in your root handler), as long as you have them in the / error / 401 Do not want to see

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -