Answer the question
In order to leave comments, you need to log in
Why is cookie-parser throwing incorrect cookies in the router?
index.js
const express = require('express');
const app = express();
const dotenv = require('dotenv');
dotenv.config();
const path = require('path');
const csurf = require('csurf');
const flash = require('connect-flash');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongodb-session')(session);
const bodyParser = require('body-parser');
const methodOverride = require('method-override');
const varMiddleware = require('./middleware/variables');
const userMiddleware = require('./middleware/user');
const Char = require('./models/char');
const cookieParser = require('cookie-parser');
const indexRouter = require('./routes/index');
const userRouter = require('./routes/user');
const settingsRouter = require('./routes/settings');
const pencalligraphy = require('./routes/pencalligraphy');
...
app.use(cookieParser())
...
app.use('/', indexRouter)
app.use('/user', userRouter)
app.use('/settings', settingsRouter)
app.use('/pencalligraphy', pencalligraphy)
app.use(function (req, res, next) {
res.status(404).render('404')
})
start();
const express = require('express');
const router = express.Router();
const Char = require('../models/char');
router.get('/', paginatedResults(Char), async (req, res) => {
let lang = req.cookies.lang;
console.log(req.cookies);
res.render('pencalligraphy', {
pageTitle: 'Basic Characters',
sessionUser: req.session.user,
bChars: res.paginatedResults,
lang
});
})
router.get('/:id', async (req, res) => {
let lang = req.cookies.lang;
console.log(req.cookies);
let char = await Char.find({char: req.params.id});
char = char[0];
console.log(char);
res.render('pencalligraphy_char', {
pageTitle: 'Basic Characters',
sessionUser: req.session.user,
char,
lang
});
})
...
module.exports = router;
const express = require('express');
const router = express.Router();
router.get('/', async (req, res) => {
let lang = req.cookies.lang;
console.log(req.cookies);
res.render('index', {
pageTitle: 'Home',
sessionUser: req.session.user,
lang
});
})
module.exports = router;
(function languageSwitcher() {
let div = document.getElementById('languageSwitcher');
let browserLang = window.navigator.language || 'en';
let currentBrowserLang = /ru/.test(browserLang) ? 'Russian' : 'English'
if (!localStorage.lang) {
div.textContent = /ru/.test(browserLang) ? 'English' : 'Russian'
} else {
div.textContent = localStorage.lang === 'R' ? 'English' : 'Russian'
}
if (!localStorage.notFirstVisit) {
document.cookie = `lang=${currentBrowserLang[0]}`;
localStorage.notFirstVisit = true;
window.location.reload(false);
}
div.addEventListener('click', (e) => {
localStorage.lang = e.target.textContent[0]
document.cookie = `lang=${e.target.textContent[0]}`;
window.location.reload(false);
})
document.querySelector('.languageSwitcher_Flag').addEventListener('click', () => {
div.click();
})
})()
Answer the question
In order to leave comments, you need to log in
When you set a cookie like this:
it is set for the current path. Those. if this code is executed on the page /pencalligraphy , then the cookie will be set for this page and subpages (for example, /pencalligraphy/about ), but not for the root page or neighboring pages.
To set a cookie for the entire site, you must explicitly specify the path for which this cookie is set. Like this:
Documentation: https://developer.mozilla.org/en-US/docs/Web/API/D...
document.cookie = 'name=value';
document.cookie = 'name=value; path=/';
document.cookie = newCookie;
[...]
;path=path
(eg, '/', '/mydir') If not specified, defaults to the current path of the current document location.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question