Kim Santos
Kim Santos

Reputation: 93

Set cookie to redirect user if set cookie exists

I have some experience with JavaScript, but I'm racking my brain with cookies. I'm trying to set a cookie for users who access page X, and then redirect them to that same page X if they visit page Y.

See a more detailed example:

When a user visits https://www.example.com/page.html, I need a cookie to be set.

When this same user visits https://www.example.com/redirect.html, I need to check if the cookie that was set on https://www.example.com/page.html exists, if it exists then this user must be redirect to https://www.example.com/page.html.

Basically it's setting a cookie, if that cookie exists then something happens. I know how to do the redirect with JavaScript, but how do I set and read this cookie?

Upvotes: 0

Views: 577

Answers (1)

Manas Dixit
Manas Dixit

Reputation: 103

You can create routes and set and receive cookies in the request and response object.

Set a cookie like this: res.cookie(cookie_name,cookie value);

Read a cookie like this: req.cookie['cookie_name'];

More about using cookies here

Upvotes: 1

Related Questions