简体   繁体   English

如何在 Meteor 中设置和获取 cookie(服务器端)?

[英]How can I set and get cookies (server side) in Meteor?

如何在 Meteor 中设置和获取 cookie(服务器端)?

Meteor does not currently have a supported way to use cookies on the server. Meteor 目前不支持在服务器上使用 cookie 的方式。

You can use cookies on the client, though.不过,您可以在客户端上使用 cookie。 Here's a snippet to show a splash screen the first time the user visits a page:这是用户第一次访问页面时显示启动画面的片段:

Meteor.startup(function () {
    if (!document.cookie.match("splash="))      
      $('body').append(Meteor.ui.render(Template.splash));      
});

Template.splash.events = {
    'click .submit': function () {      
        document.cookie = "splash=ack;expires=Sat, 23 Mar 2013 00:00:0 GMT";    
        $('#splash_outer').remove();        
    }   
};

You could use a similar approach and set the cookies in client side code, then send the results to the server in a method call.您可以使用类似的方法并在客户端代码中设置 cookie,然后在方法调用中将结果发送到服务器。

It looks like we got a solution: ostrio/cookies which work both on the server side and on the client side: https://atmospherejs.com/ostrio/cookies看起来我们有一个解决方案: ostrio/cookies 在服务器端和客户端都工作: https ://atmospherejs.com/ostrio/cookies

import { Cookies } from 'meteor/ostrio:cookies';

const cookies = new Cookies();

const oldValue = cookies.get("key");
cookies.set("key", "newValue");

2014 年 4 月更新:您现在可以使用meteor-user-session

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

相关问题 如何使用 Ajax 在 php 服务器端设置 cookie 并使用 Ajax 或 JavaScript 实时读取 cookie? - How can I set cookies in php server-side with Ajax and read cookies in the real time with Ajax or JavaScript? 流星Cookie和服务器端路由 - Meteor cookies and Server side routes 如何在流星Mongodb的服务器端插入文档? - How can I insert document on server side for Meteor Mongodb? 如何在javascript中获取服务器端cookie值? - How to get server side cookies value in javascript? 无法在Java中从应用程序的服务器端获取/设置Cookie - Cannot get/set cookies from app's server side in java 如何在服务器端设置的前端代码中检索cookie? - How to retreive cookies in front end code that is set by server-side? 在流星中,如何获取底层sockjs服务器上的句柄? - In meteor, how can I get a handle on the underlying sockjs server? Firebase:如果我使用服务器端 session cookies,如何在客户端获取登录用户的 ID 令牌? - Firebase: How do I get the ID Token for a signed-in user on the client side if I am using server-side session cookies? 如何通过Meteor.call()访问服务器端变量? - How can I access server-side variables via Meteor.call()? 如何使用meteor 在服务器端main.js 上将数据插入到我的集合中? - How can I insert data to my collection on my server-side main.js with meteor?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM