// add middleware to your endpoint
app.use((req, res, next) => {
const cookieOptions = {
domain: 'localhost',
maxAge: 1000 * 60 * 20, // valid for 20 minutes
httpOnly: false // important if you want to access this cookie using Javascript
};
res.cookie('TEST-COOKIE', 'cookie-value', cookieOptions);
res.header('Access-Control-Allow-Origin', "http://localhost:8080"); // this must be the url from which you are calling this express endpoint
next(); // must call next
});
// enable credentials from server side
var corsOptions = {
origin: "http://localhost:8080", // same url as above
credentials: true
};
app.use(cors(corsOptions)); // include before other routes
// also on your client side, you must set to use credentials. below is an example of using axios
axios.defaults.withCredentials = true;
Last active
July 30, 2019 19:15
-
-
Save keqiang/5323ef3f58de1d987380114e368482e7 to your computer and use it in GitHub Desktop.
Node Express Set Cookies for Response
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment