How to handle the server side cookies when using CookieYes?

Last updated on June 2, 2025

To handle the server side cookies on your website, first identify which categories of cookies are allowed to be set. This can be done by verifying the user’s consent state using standard cookie handling.

The sample code to handle the server side cookie while using CookieYes in some programming languages are listed below:

const express = require('express');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/', (req, res) => {
 const cookieValue = req.cookies['cookieyes-consent'];

 if (cookieValue) {
   const cookiePairs = cookieValue.split(',').map((pair) => pair.trim());
   const cookieObj = {};

   cookiePairs.forEach((pair) => {
     const [key, value] = pair.split(':');
     cookieObj[key] = value;
   });
   console.log(cookieObj);
  
 if (
     [
       'functional',
       'analytics',
       'performance',
       'advertisement',
       'other',
     ].every(
       (key) => cookieObj[key] === 'no' || !cookieObj.hasOwnProperty(key)
     )
   ) {
     console.log(
       'The user has opted out of cookies, set strictly necessary cookies only'
     );
   } else {
     if (cookieObj['functional'] === 'yes') {
       console.log('The user has accepted functional cookies');
     } else {
       console.log('The user has NOT accepted functional cookies');
     }


     if (cookieObj['analytics'] === 'yes') {
       console.log('The user has accepted analytics cookies');
     } else {
       console.log('The user has NOT accepted analytics cookies');
     }

     if (cookieObj['performance'] === 'yes') {
       console.log('The user has accepted performance cookies');
     } else {
       console.log('The user has NOT accepted performance cookies');
     }

     if (cookieObj['advertisement'] === 'yes') {
       console.log('The user has accepted advertisement cookies');
     } else {
       console.log('The user has NOT accepted advertisement cookies');
     }


     if ('other' in cookieObj) {
       if (cookieObj['other'] === 'yes') {
         console.log('The user has accepted other cookies');
       } else {
         console.log('The user has NOT accepted other cookies');
       }
     }
   }
 }
});

const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening to port ${port}`));

Was this article helpful?

Have more questions?

Reach out to us and we'll answer them.

Contact us