Reputation: 711
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
Reputation: 4795
Take a look at NowJS, which should do what you want. (Note from 2024, the project appears dead.)
Upvotes: 1
Reputation: 21
Also, Haxe, a language that lets you write using the same language for the browser and the server.
Upvotes: 2
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
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