javascript - Impossible to get cookie on a request -




i'm making express js server cookie. never worked cookie first time :)

when user login send cookie him :

res.cookie('pseudo', list[i].pseudo, {                   maxage: 1000 * 60 * 60 * 24 * 30 * 12 * 3000,                   httponly: true               }); res.cookie('email', list[i].email, {                   maxage: 1000 * 60 * 60 * 24 * 30 * 12 * 3000,                   httponly: true               }); 

when use reqest /getcookie seeing cookie have :

{ token: 'exampletoken', pseudo: 'test', email: 'test@test.test' }

my code seeing cookie :

var = function (req, res) { var cookies = require('cookies');    console.log(req.cookies); } module.exports = about; 

but when used index page no output know why ? :'(

here index code :

var index = function (req, res) { var cookieparser = require('cookie-parser'); var fs = require('fs'); require('remedial');  marqueur = {}; marqueur.pseudo = "login"; marqueur.pseudo = req.cookies.pseudo;  console.log(req.cookies); console.log(req.cookies.pseudo);  page = fs.readfilesync("./temp/index.html"); page = page.supplant(marqueur); res.writehead(200, {'content-type':'text/html'}); res.write(page); res.end(); }  module.exports = index; 

and here in server file :

var bodyparser = require('body-parser'); var cookieparser = require('cookie-parser'); var express = require('express'); require('remedial');  var app = express(); app.use(cookieparser()); app.use(express.static('temp')); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({   extended: true })); const port = 443; 

thanks !!!!

that's because haven't told express how parse cookies. cookie-parser express middleware means needs configured before attempt access cookies e.g.

const express = require('express'); const cookieparser = require('cookie-parser'); const app = express(); ... app.use(cookieparser()); // add cookie routes 




wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -