Nirav Sutariya
Nirav Sutariya

Reputation: 307

How to redirect to another page from ejs page

my code is:

<%
var result = [];
if(result.length>0)
{
 //do nothing
}
else
{
 //redirect page to "/product-not-found"
 //window.location.replace("http://stackoverflow.com"); is not working
}
%>

Note: without passing res, req from node.js controller

when i add this code:

window.location.replace("http://stackoverflow.com");

it's not working for me. show me error:

window is not defined
    at eval (eval at compile (\node_modules\ejs\lib\ejs.js:618:12), <anonymous>:215:37)
    at returnedFn (\node_modules\ejs\lib\ejs.js:653:17)
    at tryHandleCache (\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (\node_modules\express\lib\view.js:135:8)
    at tryRender (\node_modules\express\lib\application.js:640:10)
    at Function.render (\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (\node_modules\express\lib\response.js:1012:7)
    at \controllers\productController.js:1274:8
    at \node_modules\mongoose\lib\model.js:4733:16
    at \node_modules\mongoose\lib\utils.js:263:16
    at \node_modules\mongoose\lib\aggregate.js:973:13
    at \node_modules\kareem\index.js:135:16
    at processTicksAndRejections (internal/process/task_queues.js:82:9)

Upvotes: 2

Views: 11352

Answers (3)

muspi merol
muspi merol

Reputation: 128

I may be late to the party but I had this issue and this is how I solved it:

<% if (yourBoolean) { %>
  <script> document.location.href = '/your-redirect' </script>
<% } %>

Forcing a redirect via a script tag, with EJS, not a redirect from EJS.

Happy coding!

Upvotes: 0

Gabriel Arghire
Gabriel Arghire

Reputation: 2370

Until now, I didn't find a way to redirect from ejs page to another page, IF you are using data from the variabile that you should use in that ejs page.

So my solution is to redirect from the server-side, instead from the client-side. In the example below, there is a login page which, at successful login, renders the account page, and, at unsuccessful login, renders the login page again.

File: node.js

//Profile page
app.get('/account', function (req, res) {
    if(req.session.user){
        // user can access account page
        let sess = req.session;
        console.log(sess);

        res.render('account', {user: sess.user});
    }
    else{
        // user cannot access account page
        // -> display again the login page
        res.render('login');
    }
});

Upvotes: 0

Shubham Dixit
Shubham Dixit

Reputation: 1

Here is a hack you can try

1-Create a file called redirect.ejs with following code

<!DOCTYPE HTML>
<html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <meta name="robots" content="noindex, nofollow">
        <meta http-equiv="refresh" content="1;url=<%= url %>">
        <script type="text/javascript">
            window.location.href = "google.com"
        </script>
    </head>
    <body>
        If you are not redirected automatically, <a href="<%= url %>">click here</a>.
    </body>
</html>

2-Call this file as partial in your code like this

<%
var result = [];
if(result.length>0)
{
 //do nothing
}
else
{
%>
<%- include('redirect.ejs')%>// redirect to
 <%
}%>

Let me know if it works for you

Upvotes: 3

Related Questions