2

I am trying to share a specific javascript "object definition" (class) between server and client. On the server I use Node with Express and on the client Angular. (so far no database, therefore not the entire MEAN stack) The objects will be send via JSON and socket.io.

Example:

'use strict';

var Foo = function (fid) {
  this.fid = fid;

  this.toJSON = function () {
    return ('{"fid":"' + this.fid + '"}');
  };

};

Foo.fromJSON = function (json) {
  var obj = JSON.parse(json);
  return new Map(obj.fid);
};

For now the Foocode is inside a separate file. I guess I need to change my "object definition"? If yes, how? Where do I put it in my project structure? (I use this structure.)

thanks

Community
  • 1
  • 1
KenavR
  • 3,769
  • 9
  • 37
  • 47

1 Answers1

2

Put those class definitions into a file and include into the client with <script> and require with nodejs.

To achieve this on nodejs you need to pass the class def into the module.exports variable, that is only avaiable at nodejs.

var Foo = function (fid) {
  this.fid = fid;

  this.toJSON = function () {
    return ('{"fid":"' + this.fid + '"}');
  };    
};

if(module && module.exports)
   module.exports = Foo;

Then you can use it in nodejs:

Foo = require("foo.js");
var foo = new Foo();

This solution works, if you place every class in an own file.

If you want to have all classes in one file:

var Foo = function (fid) {
  this.fid = fid;

  this.toJSON = function () {
    return ('{"fid":"' + this.fid + '"}');
  };    
};

var Bar = function (fid) {
  this.fid = fid;

  this.toJSON = function () {
    return ('{"fid":"' + this.fid + '"}');
  };

};
if(module && module.exports){
   module.exports.Foo = Foo;
   module.exports.Bar = Bar;
}

In you nodejs:

var Classes = require("classes.js");
var foo = new Classes.Foo();

Update due to the question in the comment:

To use the Foo inside the Bar you need to require this in nodejs and in client you dont need anything (global).

var Bar = Bar || require("Bar.js");  //if class Bar is undefined, we work on nodejs and need to require the Bar file.

function Foo (){
  var bar = new Bar();
}

if(module && module.exports){
  module.exports.Foo = Foo;
}
Konstantin Krass
  • 8,626
  • 1
  • 18
  • 24
  • Thanks. For now I only tested the import on the server side, but I stumbled upon a new "problem". Based on your example, what if I want to use 'Bar' inside 'Foo' (seperate files)? I could check for "module" or "module.exports" and add the dependency based on the result, or is there a better way? – KenavR Apr 24 '14 at 20:53
  • i would build a method, that does return the required class. See my update – Konstantin Krass Apr 25 '14 at 12:08
  • Thanks again. Now I also tried using it on the client, but I see "Uncaught ReferenceError: module is not defined" in the console. I understand why "module" is not defined on the client, but I tought "if(module)" would prefend the output. '(function (module) { if (module && module.exports) { module.exports = Foo; } }());' that works, but is this the correct way? – KenavR Apr 28 '14 at 13:08
  • maybe check `typeof module != "undefined"` – Konstantin Krass Apr 28 '14 at 13:16
  • I checked "module !== undefined" but that didn't work. Yours on the other hand does work, kinda weird (at least for me) but thanks. – KenavR Apr 28 '14 at 13:24