not able to set the cookie from script tag javascript nodejs
Asked Answered
L

1

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

Len answered 2/2, 2022 at 7:37 Comment(2)
did you try with cookieParser? https://mcmap.net/q/108057/-how-can-i-set-cookie-in-node-js-using-express-frameworkProphecy
@MWO, yes i am using cookie parserLen
P
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

Prophecy answered 2/2, 2022 at 9:18 Comment(7)
i want to read that cookie inside the browserLen
i tried the same as try 1 but not able to read the cookie on browser sideLen
i can read it. in network pane of dev toolsProphecy
yes i can see in the network tab, can i read it in a variable? document.cookie is emptyLen
as you can see in the update, you can see it in the chrome cookies storage.Prophecy
but still i can not read the cookies using document.cookie, and now if browser make any other call , then on nodejs side i am not able to get cookies by req.headers.cookieLen
i think its because its a cors request, it does not save it in document.cookies https://mcmap.net/q/324686/-setting-cookies-via-ajax-cors-response-and-accessing-them-in-document-cookiesProphecy

© 2022 - 2024 — McMap. All rights reserved.