How to redirect any page to a single page?



  • If there is a specified key in the session, I want any page to be redirected to the page I specified.
    For example, If user visit /recent /topic,it will be redirected to /register(if session is set.)
    I tried to use the hook filter:middleware.render.If enter the page directly, it works well.But if you change page via ajax(click Home button etc.), it will throw a error : res has been sent.

    I also tried the hook filter:router.page.But I don't know how it works. I search for this hook in Github, But I can't find something useful.

    Could you give me any help? I don't know how to solve it exactly.I will appreciate it if you help me.


  • Admin

    Hi @a632079 -- session-sharing uses filter:router.page. You could probably take a look and see how it is used there.

    It is used in a "middleware" style, so it passes in req res and next. Just call res.redirect('/route'); if you want to redirect. No need to call next unless nothing in your script does anything (and you want NodeBB to continue processing).



  • @julian I am troubled by this problem for many days. Now I can continue to develop. Thank you very much



  • @julian First, Thank you very much. but after testing, I found it's still unable to let ajax request to redirect.
    0_1509631385290_01f996d1-31c2-4a18-beda-12c1b1e86f4d-image.png


    To solve it, should I use res.render('template') or use filter:middle.render to add a script to redirect?


  • Admin

    @a632079 Ah, that is a more specific workflow, if you need to handle both cold loads and API, you'll need to do something like this:

    helpers.redirect(res, '/login');
    

    Instead of calling res.redirect by itself.



  • @julian Ahhh, It's great! Thanks.


  • Admin

    No problem. I forget it all the time too 😆


 

Suggested Topics

| |