简体   繁体   English

仅当 cookie 存在时如何获取 URL 否则重定向到不同的 url?

[英]How to get URL only if cookie exists else redirect to different url?

A cookie has been created in EJS file named click=click11.在名为 click=click11 的 EJS 文件中创建了一个 cookie。 I want whenever someone goes to url first check whether the cookie is there or not.我想每当有人去 url 时首先检查 cookie 是否存在。 If its there render a page if it isn't there render different page.如果它在那里呈现一个页面,如果它不在那里呈现不同的页面。

Here is my code.这是我的代码。 Which I want to render if the cookie exists.如果 cookie 存在,我想渲染它。 Else send error?否则发送错误? How to implement?如何实施?

router.get('/:uuid_d', async (req, res) => {
 const file = await File.findOne({});
 const user = await User.findOne({});
 if(!file) {
      return res.render('download'});
 }

 const response = await file.save();
 const response2 = await user.save();
 const filePath = `${__dirname}/../${file.path}`;

res.download(filePath);
});

in NodeJs in order to get the cookies you have to set在 NodeJs 中为了获得 cookies 你必须设置

      const cookieParser = require('cookie-parser') 
      app.use(cookieParser())

to Get the cookie获取 cookie

      router.get('/:uuid_d', async (req, res) => {
        const cookies = req.cookies
        const SpecifiedCookie = req.cookies.YourCookieName
         //check if cookie exists
         if(SpecifiedCookie) console.log('true') 
      }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript 每小时创建 cookie,如果 cookie 不存在,并且如果 cookie 存在:请重定向到其他 url - Javascript create cookie every hour, if cookie not exists, and if cookie exists : do redirect to other url angularjs:如何重定向到不同的网址? - angularjs: how to redirect to a different url? 仅当cookie存在时addClass否则隐藏 - addClass only if cookie exists else hide URL重定向后如何获取新的URL - How to get the new URL after URL redirect 将 URl 添加到 cookie 并在返回时重定向到 URL - Add URl to cookie and redirect to URL on return JS - 如何获取重定向的URL - JS - How to get URL of redirect 仅获取当前网址的一部分,并使用javascript重定向 - get only part of current url and redirect with javascript 如何创建onclick cookie链接,然后导航到链接的url之后,让该cookie重定向当前url - How to create an onclick cookie link and then after navigating to link's url, have that cookie redirect the current url 如何将当前的 URL 存储在 cookie 中,并使用该 cookie 检查其他页面,如果可用,然后重定向到 JS 中的相同 URL? - How to store current URL in cookie, and use that cookie to check in other page, if available then redirect to the same URL in JS? 如果URL仅在基本URL上,则重定向 - if URL is only at base URL then redirect
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM