简体   繁体   中英

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.

You can use cookies on the client, though. 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.

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

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

const cookies = new Cookies();

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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