Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor: are code inside Meteor.isServer block readable from client?

Tags:

meteor

I am confused as to when you would use the isServer block to put stuff in. obviously, database calls and etc. Does this show up in the client browser?

like image 873
KJW Avatar asked Mar 23 '13 01:03

KJW


2 Answers

I would advise against using that instead put your stuff in folders as advised on the unofficial meteor faq

Even if you use if (Meteor.isServer) {...} this block will still be sent down to the client if you don't use the folder structure above i.e putting it in /server. But it will ignore all the code inside it.

On the server side code you would place code you only want to run on the server, i.e Publish functions, and data that would be more sensitive & the user shouldn't have access to.

like image 65
Tarang Avatar answered Oct 26 '22 20:10

Tarang


I think it does get sent down to the client, unless it's in the server folder. In response to one of the comments about sharing global variables, I've been using this pattern. Create the same global variable in client and server folders respectively, and once outside of those folders for any shared code. Initialize the global variable in each place by testing to see if it already exists.

MyVar = typeof MyVar === 'undefined' ? {} : MyVar;

Then, just put methods where you need them. For instance, I'll have a User object with a method that tests to see if the user is authorized. I'll declare the method once on the server global User, and once on the client global User. The methods are different because the server version checks for custom properties on the user object which aren't available on the client. Then, in a Meteor.methods method, which runs on both the client and server, you can invoke the authorization method, and it will call different methods depending on if it's running on the client or server.

like image 29
rhythnic Avatar answered Oct 26 '22 21:10

rhythnic