Sharing code between client and server

517 views
Skip to first unread message

Robert Chris Bang Larsen

unread,
May 8, 2012, 5:05:26 AM5/8/12
to nodejs
Hi all

I am planning on using Node.js for the server side of multiplayer
games based on JavaScript and HTML5. I used to use Java for this and
much code could be shared between client and server, for instance most
of the game model. A Backgammon client would show the user which moves
were legal, and the server would verify the legality of a requested
move, thus they both need a way to enumerate valid moves.

But how can I do this when using Node.js ?

Building a module with 'module.exports' and 'require' will mess up the
client side, and having just plain JavaScript won't work with Node
because something will have to be exported from the file (am I
wrong ?).

So, how would you go about having shared code between Node.js and the
client side ?

Best,
Robert

mscdex

unread,
May 8, 2012, 5:13:31 AM5/8/12
to nodejs
On May 8, 5:05 am, Robert Chris Bang Larsen <rob...@komogvind.dk>
wrote:
> So, how would you go about having shared code between Node.js and the
> client side ?

If the code is standalone and doesn't need anything specific to node
or doesn't require other modules (although something like browserify
might help with that in that case), you can simply just do a check for
`module.exports` at the end of your script. If it exists, then add
your functions and whatnot to that so that it can be used as a module,
otherwise if `module.exports` is not defined, attach functions, etc.
to something like `window`.

That's generally how I've shared validation logic and other "plain
javascript" stuff.

Rob Ashton

unread,
May 8, 2012, 5:13:10 AM5/8/12
to nod...@googlegroups.com
See

browserify
requirejs
stitch
everything else under 'module loader'

There is a thread somewhere a week or so ago with a list of these in them, I personally just use RequireJS even though it isn't cool


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Axel Kittenberger

unread,
May 8, 2012, 5:37:26 AM5/8/12
to nod...@googlegroups.com
In my project a typical structure of a file shared between client and
server looks like this.

This avoids using any precompilers for client side in development,
thus you get useful error messages with linenumbers just as they are.
It uses shared globals on browser javascript and proper requires for
server. For release mode I just concat all client files into one big
file, push it through uglify and gzip it. The order in which the files
are concated or added as <script> into the devel html can be important
for subclassing or so, but I just work that manually out.

This would be for "ModuleF" exporting the prototype "ModuleF"

/**
| Imports
*/
var ModuleA;
var ModuleB;
var ModuleC;

/**
| Exports
*/
var ModuleF;

/**
| Capsule
*/
(function() {
"use strict";

/**
| Node includes.
*/
if (typeof(window) === 'undefined') {
ModuleA = require('./modulea');
ModuleB = require('./moduleb');
MoudleC = require('./modulec');
}

ModuleF = function(bla) {
// code
};

ModuleF.prototype.foobar = function() {
// code
};

// .. and so on ...

/**
| Node
*/
if (typeof(window) === 'undefined') {
module.exports = ModuleF;
}

}());

Alexey Petrushin

unread,
May 8, 2012, 11:33:38 AM5/8/12
to nod...@googlegroups.com
Take look at browserify or brunch

P. Douglas Reeder

unread,
May 8, 2012, 3:02:42 PM5/8/12
to nod...@googlegroups.com
Mojito from Yahoo supports this (but I haven't used it myself). I believe you need to use YUI for your widgets with Mojito.

Azer Koçulu

unread,
May 9, 2012, 11:27:01 AM5/9/12
to nod...@googlegroups.com

I would recommend OneJS (http://github.com/azer/onejs) since it's a better implementation of commonjs and lets you separate your client-side project as an independent commonjs package. I built multiplayerchess.com using onejs last year. You may want to take a look at its source code: http://github.com/azer/multiplayerchess.com

Mariusz Nowak

unread,
May 10, 2012, 1:59:02 PM5/10/12
to nod...@googlegroups.com
https://github.com/medikoo/modules-webmake

It works, it's fast, Currently I'm building all client/server web applications with that (on average I have 60% of modules shared between server and client)

Robert Chris Bang Larsen

unread,
May 15, 2012, 6:41:47 AM5/15/12
to nod...@googlegroups.com
Thanks for all the suggestions. I tried detecting which environment I was in but it didn't work.

We have built a couple of singleplayer games and built our own framework for this using the Google Closure library and compiler, so this would have to be supported too.

I tried doing the following:

(function() {
    if (goog) {
        //Client
        goog.provide('multigame.model.Ball');
    } else {
        //Server
        multigame = { model: {} };
        module.exports = multigame.model;
    }
})();

/**
 * @constructor
 */
multigame.model.Ball = function(x, y, r) {
    this.x = x;
    this.y = y;
    this.r = r;
};


This works fine with the Closure compiler, but Node breaks with a 'ReferenceError: goog is not defined'.
If I check against 'module' and 'module.exports' then Node works but the Closure compiler breaks with 'ERROR - variable module is undeclared'

So...how do I check wheter or not something that might not be defined is defined without getting errors ?
Will I really have to translate the code in order to compile/run it ?

Mariusz Nowak

unread,
May 15, 2012, 6:57:31 AM5/15/12
to nod...@googlegroups.com
It's rather JavaScript question, you should do:

if (typeof goog !== 'undefined') {

Davis Ford

unread,
May 17, 2012, 1:14:58 AM5/17/12
to nod...@googlegroups.com
On Tue, May 8, 2012 at 5:05 AM, Robert Chris Bang Larsen
<rob...@komogvind.dk> wrote:
> Hi all
>
> I am planning on using Node.js for the server side of multiplayer
> games based on JavaScript and HTML5.
>
> But how can I do this when using Node.js ?
>
> So, how would you go about having shared code between Node.js and the
> client side ?
>

Take a look at SocketStream http://socketstream.com - it is perfect
for something like a real-time multiplayer game. I'm using it right
now, and loving it. Sharing code between client/server is easy. See,
for example, the section on 'system' libraries:
https://github.com/socketstream/socketstream/blob/master/doc/guide/en/client_side_code.md

You could, of course build this yourself with browserify, etc., but
socketstream has already done it for you, plus a lot of other great
stuff.

Regards,
Davis

--
http://daisyworks.com
Reply all
Reply to author
Forward
0 new messages