#if client
#if server
#endif
for the pieces that only apply to the client side or the server side, with shared pieces staying outside these "tags". I won't go back. This is especially cool as I have a templating system that can run on the server or on the client. So for JS-enabled browsers, all HTML template rendering is done on the client by building all HTML tags out of very small simple JSON structs. For non-JS clients (spiders, bots, paranoid non-JS enterprise legacy browsers) the "pages" get fully rendered old-school by the server-side -- both sharing the same rather simple custom-made JSON-driven templating functions and producing essentially almost the same HTML output. Of course there are some added complexities going on with things like scripted onclicks, but you get the general idea. (This is mostly done for indexing spiders and human non-JS clients just simply have to put up with a limited, degraded, basic experience.)
- Input Validation
- Asynchronous loop patterns
- Promise pattern implementations
- markup logic & output
- A* pathfinding
More and more use cases will pop up over time, and it's really unique to the node.js environment. This alone would not make me pick node.js over another solution but I feel it is still an advantage.
I spend about 50% of my dev time on writing / working with utility libraries as writing these libraries takes far more effort than writing handlers, models, etc.
Validation on the client is primarily a user experience concern,
where you focus on bringing attention to problems with a form, and
helping the user resolve those problems.
Once the form is submitted, if you’ve done thorough client-side
validation you can also avoid friendly server-side validation. Of
course all your client-side validation could be avoided through a
malicious client, but you don’t need to give a friendly error
message in that case, you can simply bail out with a simple 400 Bad
Request error.
At that point there’s not much in common between these two kinds of
validation — the client is all user experience, and the server is all
data integrity.This is not easy by just using Node, and it's not necessary that the language you use is Javascript. A better approach IMO is that you have a language X that is nicer than Javascript, and compiles both to server side code and client side Javascript.
For the case of validation here is a hypothetical language (ignore the bad regex):
<form action="/foo/bar">
<input type="text" name="email" />
<%= if not email ~ "[a-z]+@[a-z]+.[a-z]+": invalid("Email is not valid") %>
<submit>
</form>
From this code you could generate real time JS validation and server side validation.Another example: validation. You want to validate on the client for responsiveness, but on the server for integrity.
Being able to reuse content generating code can be especially useful when you don't realise that you will need it on both the client and server when you write it, or you have to move it from one to the other.
Otherwise, you really have no excuse for putting logic on the client. Even with something like validation, I think its much more sensible to have your renderers pump out generic validation event handlers than to reconstruct the whole model class on the client.
Obviously then you'd need some way of distinguishing validation that is only important to one of the two ends from that which is meaningful to both, but I can see it being possible.
That said, I'm still not convinced it's worth it. Especially when you look at the state of libraries in node compared to something like Python/Ruby.
These can just as easily be described using RDFa as using TAL or Jade or whatever. The benefit of RDFa is that the templating information stays with the page, so client-side can also utilize it.
The library I made for this is https://github.com/bergie/VIE
1) Utility libraries. I have a clone method I use a lot, an would work equally well in both environments. I'm sure math libraries would be the same way.
2) Models. Maybe. Personally, my server side models contain get and save methods which make database calls. I don't want or need those on the client side. If for no reason other than wasted bandwidth.
* note: I'm a node user, but I don't share any code between server and browser.