Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NodeJs Express send 403 and render

How can I send a error 403 and render a page with 'you have no rights to visited this page' message?

I have now this:

res.send(403,"You do not have rights to visit this page");

but I want to render a HTML page instead a basic text

res.render('no-rights', {title: 'You have no rights to visit this page', text: 'You are not allowed to visited this page. Maybe you are not logged in?'});

with a 403 status.

like image 982
NVO Avatar asked Aug 10 '16 12:08

NVO


2 Answers

http://expressjs.com/en/api.html#res.status

res.status(403);
res.render();

Or in one line

res.status(403).render();
like image 127
Mykola Borysyuk Avatar answered Sep 27 '22 16:09

Mykola Borysyuk


As you can see in the error handling page of Express you can set the status first and then render the page.

  res.status(500);
  res.render('error', { error: err });

similarly I would create a page that could inform the user about the 4xx (client error) and 5xx (server error) errors in a similar way by passing the status code and the title as parameters.

like image 24
cs04iz1 Avatar answered Sep 27 '22 15:09

cs04iz1