Onedayitwillmake
Onedayitwillmake

Reputation: 711

Sharing a class between Node.js and the browser

If i have a class, what is the best way to share that class and use it in both the browser as well as Node.js?

For example a game that uses a 2D Vector math class on both the client-side and the node.js.

Upvotes: 3

Views: 865

Answers (4)

Mike Scott
Mike Scott

Reputation: 4795

Take a look at NowJS, which should do what you want. (Note from 2024, the project appears dead.)

Upvotes: 1

Dion
Dion

Reputation: 21

Also, Haxe, a language that lets you write using the same language for the browser and the server.

Upvotes: 2

Mariusz Nowak
Mariusz Nowak

Reputation: 32848

Write it as CommonJS/Node.js module, so it works normally in Node.js, and then pack written module for browser using Webmake. It's simplest way to share same codebase on both sides

Upvotes: 0

entropo
entropo

Reputation: 2481

NowJS is a pretty elegant way to share variables and functions between client JS and Node JS through an automatically sync'ed shared namespace.

There's also dnode, which the author comments on in response to a posting about NowJS here: http://news.ycombinator.com/item?id=2316079

Upvotes: 3

Related Questions