Hubro
Hubro

Reputation: 59343

How should I go about writing a node.js web application with both server and client side code?

I'm planning on writing a spine/backbone.js style web application which basically just transfers a large application.js file to the client's browser that communicates with the node.js backend using ajax. The problem is that I don't know how to structure such a project, since I've never seen examples of such an application. I can picture some pros and cons with different ways of doing this

Any thoughts?

Upvotes: 5

Views: 894

Answers (4)

saurshaz
saurshaz

Reputation: 509

Things have moved much ahead now, and things like

browserify influenced coding can help us achieve this easily

there will always be some uncommon code between server and client sides, But the goal shall always be to keep all the logic code in different modules(which are later used from both environments). This is better from TDD point of view as well, also keeps your keyboard press count to lesser.

Have a look at things like this stack - http://mindthecode.com/lets-build-an-angularjs-app-with-browserify-and-gulp/

Having said that your option1 did not seem that manageable to me, if you had the right coders coding the right code.

Upvotes: 0

Jasper
Jasper

Reputation: 2471

Was realtime required? Otherwise the Derby approach might be a little too heavy. Express.js proposes a structure where client js is separated in public folder, and provides methods to get a quick RESTful API running, which you can then access with your application.js. I guess you could load "classic" js files from public into node via eval() too.

Upvotes: 0

Timothy Meade
Timothy Meade

Reputation: 2506

This won't be a complete answer to your question, but one library that might help if you choose to pursue such an endeavour might be Browserify.

It's designed so you can use a similar require() function with a preprocessed, or on-the-fly generated from module source, js file containing many different modules. These modules can be shared with the server side through the same require() mechanism.

I don't know explicity of a Backbone implemented on the server side as a server side counter part for model sync, that would seem to be the first goal you are looking for, aloowing code that makes sense to be shared, such as models and validation, to be usefully shared.

Another thing to look at is requirejs, which uses more traditional script tag asynchronous loading f js modules, but also works within node.js aloowing the same AMD modules to a be shared between node and client code.

Upvotes: 0

glortho
glortho

Reputation: 13198

Your first situation is a very tricky scenario and I would suggest that we're not quite there yet. Some would argue that there's little reason to try to get there, as front/back ends will always be tasked with slightly and sometimes drastically different tasks. Libraries like derby show promise, but aren't quite there yet.

I discussed this recently with a friend and we came to the conclusion that perhaps the best bet for now would be to serialize models over websockets, and then ensure that the node server and client app stay in sync.

I may work on such a library, but for now I'm still developing with 2 folders and copies of models on both sides. Layout mark-up gets sent from the server, with all other content rendered client-side after receiving JSON from the server. Frankly, the amount of duplication isn't really that substantial. A little irritating but also maintains greater flexibility to grow in different directions.

Upvotes: 3

Related Questions