简体   繁体   English

无法获取 cookie nodejs

[英]couldn't get the cookie nodejs

I set a cookie with,我设置了一个cookie,

router.get("/addCartToCookie", function(req, res) {
  let options = {
    maxAge: 1000 * 60 * 15,
    httpOnly: true, 
  };

  let cartData = {
    name: "test cookie",
    slug: slugify("test cookie"),
    productPictures: "K6B4dzGjN-teal.jpeg",
    price: 200,
    description: "testing cookies",
    quantity: 7,
  };

  // Set cookie
  res.cookie("cartName", cartData, options); 
  res.send("Cart Added to Cookie");
});

Which perfectly sets a cookie named "cartName".这完美地设置了一个名为“cartName”的cookie。 But once I try to get the cookie, It shows "cartName" is undefined.但是,一旦我尝试获取 cookie,它就会显示“cartName”未定义。

Get Cookie Code:获取 Cookie 代码:

router.get("/getCartFromCookie", function (req, res) {
  res.send(req.cookies["cartName"]);
  console.log(req.cookies);
});

I tried console logging the cookies but it also shows undefined.我尝试控制台记录 cookies 但它也显示未定义。

From what I know, the optimized way of handling cookies is by using cookie-parser dependency for express.据我所知,处理 cookies 的优化方法是使用cookie-parser依赖来表达。

$ npm install cookie-parser

Then you could easily fetch your cookies using req.cookies or req.signedCookies property like below:然后,您可以使用req.cookiesreq.signedCookies属性轻松获取 cookies,如下所示:

var express = require('express')
var cookieParser = require('cookie-parser')

var app = express()
app.use(cookieParser())

app.get('/', function (req, res) {
  // Cookies that have not been signed
  console.log('Cookies: ', req.cookies)

  // Cookies that have been signed
  console.log('Signed Cookies: ', req.signedCookies)

  // Your cart cookie
  console.log('Cookies: ', req.cookies.cartName)
})

app.listen(8080);

References:参考:

  1. https://github.com/expressjs/cookie-parser https://github.com/expressjs/cookie-parser
  2. http://expressjs.com/en/resources/middleware/cookie-parser.html http://expressjs.com/en/resources/middleware/cookie-parser.html

You don't show all your code, but I assume you're using thecookie-parser middleware to access the cookies directly on the request object.您没有显示所有代码,但我假设您正在使用cookie-parser 中间件直接在请求 object 上访问 cookies。 If not, start by adding cookie-parser and go from there.如果没有,请从那里添加 cookie-parser 和 go 开始。

For your cookie reading issue, be aware that you can't send and read cookies on the same request.对于您的 cookie 读取问题,请注意您无法在同一请求上发送和读取 cookies。 You need one request to set the cookie and another to read the cookie.您需要一个请求来设置 cookie,而另一个请求来读取 cookie。

import express from 'express';
import cookieParser from 'cookie-parser'
import {inspect} from 'util';

const app = express();
const router = express.Router()
router.use(cookieParser())
app.use(router);

const port = 3000

router.get('/setcookie', (req, res) =>{
  res.cookie('testCookie', 'magic content');
  res.send("set the cookie");
})

router.get('/readCookie', (req, res) =>{
  res.send('your cookies: ' + inspect(req.cookies['testCookie']));
  console.log(req.cookies);
})

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM