I'm trying to create a cookie that would be equal to 1. but when console log- it's undefined all the time! is someone recognizing where is the error in here?
var express = require('express');
const http = require('http')
const port = 4000;
var cookieParser = require('cookie-parser'); // module for parsing cookies
const cors = require('cors');
var app = express();
const bodyParser=require('body-parser');
var corsOptions =
{
origin: 'http://localhost:3000',
optionsSuccessStatus: 200,
allowHeaders: ['sessionId', 'Content-Type'],
exposedHeaders: ['sessionId'],
credentials: true,
}
app.use(cors(corsOptions));
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(cookieParser());
const server= http.createServer(app)
app.post("/login", async (req, res) => {
let details=req.body.loginObj;
let cookieChecker=1;
res.cookie('userData', cookieChecker, { maxAge: 900000, httpOnly: true });
let cookiex= req.cookies['userData'];
console.log('the cookie!',cookiex) //retreive "Undefined"
res.send(cookiex);
res.end("");
});
server.listen(port, function () {
console.log('11.2 ',port);
})
Thank you for helping!