Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Meteor backend code always available on client-side?

I created test Meteor application and I found that overall code (server-side too) available to look with dev tools on a client. Test app (In browser):

 (function(){ if (Meteor.isClient) {
      Template.hello.greeting = function () {
        return "Welcome to test_app.";
      };

      Template.helo.events({
        'click input' : function () {
          // template data, if any, is available in 'this'
          if (typeof console !== 'undefined')
            console.log("You pressed the button");
        }
      });
    }

    if (Meteor.isServer) {
      Meteor.startup(function () {
        // code to run on server at startup
      });
    }

    }).call(this);

Is this by design? Can server-side code stay at server?

like image 663
Denys Lieu Avatar asked Aug 09 '13 08:08

Denys Lieu


People also ask

Is Meteor JS frontend or backend?

It is a Full Stack Framework The developers can take advantage of the features provided by Meteor. js to create both the backend and frontend user interface of the application rapidly.

What does it mean that JavaScript is performed on the client side?

Client-side scripting simply means running scripts, such as JavaScript, on the client device, usually within a browser. All kinds of scripts can run on the client side if they are written in JavaScript, because JavaScript is universally supported.


1 Answers

If you want to keep server side code on the server you have to restructure your app.

Make these directories in the root directory of your application:

  • /server - stores everything to run on the server only
  • /client - stores everything to run on the client only
  • /public/ - stores anything that should be accessible in at http://yoursite/ (i.e images, fonts). If you place an image a.jpg in /public it will be available at http://yoursite/a.jpg

Once you use this structure you don't have to use the if(Meteor.isServer) {..} or if(Meteor.isClient) {..} conditions anymore as they would run in the right place.

When you put files in the root directory of your meteor application, they would run on both the client and the server so this is why the file is unaltered & everything in the if(Meteor.isServer) would only run on the server.

It is by design and quite helpful to share code between the server and client, though it would be visible to both the client & server

like image 179
Tarang Avatar answered Nov 08 '22 22:11

Tarang