0

I'm trying to add a simple JS script to the server side of Meteor. Everywhere I read I'm told I should create an atmosphere package for the script-but that seems like a pretty round about way of doing it.

I am presently creating a local package to extend a feature on the app I'm using, and would like to use the script on the server side. Is there a way to simply REQUIRE a js file in meteor?

JHAWN
  • 399
  • 4
  • 18

3 Answers3

0

You can simple create server folder and add a js file to it. Or you can use everywhere

if (Meteor.isServer) 
{
   //some servercode
}

more info about project structure http://meteortips.com/tutorial/structure-application/

polacekpavel
  • 431
  • 3
  • 9
0

The simple way to REQUIRE a js file in meteor is to put in the lib folder.

If it's required only by the front-end, then place it in the client lib folder (myproject/clinet/lib/requiredJSfile.js), if it's required by only the server, then place it in the server lib folder (myproject/server/lib/requiredJSfile.js). If it will be used by both, put it in the root lib folder (myproject/lib/requiredJSfile.js).

You should do this, because the meteor rendering engine places the files from the lib folder on top of the inclusion list, meaning that when your actual meteor code runs it will be already available.

Check out this boilerplate for an example: https://github.com/matteodem/meteor-boilerplate

Benedek Gagyi
  • 18
  • 1
  • 5
0

As a new user of Meteor it's not super clear here that, when creating a function to be included server-side you write it differently in Meteor.

Traditional .js function declaration would look something like:

function serverFoo(param1) {
   console.log("serverFoo() param1="+param1);
   return "bar";
}

The way to declare this function in Meteor:

Create a file "server/inc/server-globals.js".

var serverFoo = function(param1) {
   console.log("serverFoo() param1="+param1);
   return "bar";
}