1

On the Html Side

<script src="http://localhost:3002/widget-load?clientId=1" type="text/javascript">

And on the nodejs

app.get('/widget-load', function (req, res) {
   const { clientId } = req.query;

   // Try 1
   res.cookie("clientId", clientId, {
        // httpOnly: true
   });

   // Try 2
   res.setHeader('Set-Cookie', `clientId=${clientId};`);
   res.sendFile(__dirname + '/public/static/js/widget-load.js');

});

but this is not working. Even I tried

app.use(function (req, res, next) {
    res.header('Access-Control-Allow-Credentials', true);
    res.header('Access-Control-Allow-Origin', req.headers.origin);
    res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With, X-HTTP-Method-Override, Content-Type, Accept');
    next();
});

even i am using cookie-parser

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

but still this is not working. No cookie is getting set on browser

2
  • did you try with cookieParser? stackoverflow.com/a/16209531/9175097 Commented Feb 2, 2022 at 7:47
  • @MWO, yes i am using cookie parser Commented Feb 2, 2022 at 8:28

1 Answer 1

1

With a combination from the answer in my link, and your requierments, it is working here, cookie is set to clientId=1. For cross site resources you have to set the options to sameSite: 'none' and secure: 'false' if you are not running ssl.

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

app.get('/widget-load', function (req, res) {
    const { clientId } = req.query;
    var cookie = req.cookies.clientId;
    if (cookie === undefined) {
      res.cookie('clientId',clientId, {httpOnly: true, sameSite: 'none', secure: 'false' });
      console.log('cookie created successfully');
    } else {
      // yes, cookie was already present 
      console.log('cookie exists', cookie);
    } 


    // // Try 1
    // res.cookie("clientId", clientId, {
    //      // httpOnly: true
    // });
 
    // // Try 2
    // res.setHeader('Set-Cookie', `clientId=${clientId};`);
     res.sendFile('widget-load.js',{'root':'public'});
 
 });

 app.listen(3002, () => {
    console.log(`Example app listening on port 3000`)
  })

enter image description here

enter image description here

Sign up to request clarification or add additional context in comments.

7 Comments

i want to read that cookie inside the browser
i tried the same as try 1 but not able to read the cookie on browser side
i can read it. in network pane of dev tools
yes i can see in the network tab, can i read it in a variable? document.cookie is empty
i think its because its a cors request, it does not save it in document.cookies stackoverflow.com/a/18769954/9175097
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.