简体   繁体   English

如何从服务器端访问使用res.cookie设置的cookie到客户端浏览器,现在想将它们访问到服务器端

[英]How to Access cookies set with res.cookie from server side to the client browser, and now want to access them to server side

I am trying to get it with document.cookie.split(';');, then i am getting an error as let user = document.cookie.split(';'); 我试图用document.cookie.split(';');来获取它,然后由于用户= document.cookie.split(';');而出现错误。 ^ ^

ReferenceError: document is not defined at Object. ReferenceError:未在对象处定义文档。 (/home/codemymobile/study/trello/public/card-controller.js:3:12) at Module._compile (internal/modules/cjs/loader.js:778:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10) at Module.load (internal/modules/cjs/loader.js:653:32) at tryModuleLoad (internal/modules/cjs/loader.js:593:12) at Function.Module._load (internal/modules/cjs/loader.js:585:3) at Module.require (internal/modules/cjs/loader.js:692:17) at require (internal/modules/cjs/helpers.js:25:18) at Object. (/home/codemymobile/study/trello/public/card-controller.js:3:12)位于Object.Module._extensions..js的Module._compile(internal / modules / cjs / loader.js:778:30) (internal / modules / cjs / loader.js:789:10)在try.ModuleLoad(internal / modules / cjs / loader.js:593:12)在Module.load(internal / modules / cjs / loader.js:653:32) )于Function.Module._load(internal / modules / cjs / loader.js:585:3)于Module.require(internal / modules / cjs / loader.js:692:17)于require(internal / modules / cjs / helpers.js:25:18)。 (/home/codemymobile/study/trello/index.js:8:22) at Module._compile (internal/modules/cjs/loader.js:778:30) (/home/codemymobile/study/trello/index.js:8:22)在Module._compile(内部/模块/cjs/loader.js:778:30)

document refers to the dom object in the browser. 文档是指浏览器中的dom对象。 you cannot use it while running it on node. 您无法在节点上运行它时使用它。

First install cookie-parser and then 首先安装cookie解析器,然后

var cookieParser = require('cookie-parser');
app.use(cookieParser()); //add this on server setup

Now you can access the cookies using 现在您可以使用

req.cookies["cookieName"]

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

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