views:

423

answers:

5

Is the use of server side javascript prevalent? Why would one use it as opposed the any other server side scripting? Is there a specific use case(s) that makes it better than other server side languages?

Also, confused on how to get started experimenting with it, I'm on freeBSD, what would I need installed in order to run server side javascript?

+4  A: 

I think a really cool use of server-side Javascript that isn't used nearly often enough is for data validation. With it, you can write one javascript file to validate a form, check it on the client side, then check it again on the server side because we shouldn't trust anything on the client side. It lets you keep your validation rules DRY. Quite handy.

Also see:

Paolo Bergantino
+1: You know, I never thought I would ever vote up anything in favour of server-side javascript, but your answer is good food for thought!
Jarret Hardie
+17  A: 

It goes like this:

Servers are expensive, but users will give you processing time in their browsers for free. Therefore for big sites server-side code is relatively expensive compared to client-side code. However, there are some things like data validation and retrieval that you can't leave to the client. You'd like to do them on the client, because it means faster response times for the users and less server infrastructure for yourself, but security and accessibility concerns mean that server-side code is required.

What typically happens is that you do both. You write your server side logic because you have to, but you also write the same logic in javascript in the hopes of providing faster responses to the user and saving your servers a little extra work in some situations.

Now since we're all (mostly) programmers here we should immediately spot the new problem. There's not only the extra work involved in developing two sets of the same logic, but also the work involved in maintaining it, the bugs that inevitably result because the platforms don't match up well, and the bugs introduced as the implementations drift apart a little over time.

Enter server-side javascript. The idea is that you can write code once, so that the same code runs on both the server and the client. This would appear to solve most of the issues. You get the full set of both server and client logic done all at once. No drifting, no double maintenance. It's also nice when your developers only need to know one language for both server and client work.

Unfortunately, in the real world it doesn't work out so well. The problem is three-fold:

  1. The server view of a page is just too different from the client view of a page. The server needs to be able to do things like talk directly to a database that just shouldn't be done from the browser. The browser needs to do things like manipulate a DOM that doesn't match up with server.
  2. You don't control the javascript engine of the client, meaning there will be important language differences between your server code and your client code.
  3. The database is normally a bigger bottleneck than the web server, so savings are minimal.

It's not an insurmountable technical problem: you constrain the server-supported language to a sub-set of javascript that's well supported across most browsers, provide an IDE that knows this subset and the server-side extensions, make some rules about page structure to minimize DOM issues, and provide some boiler-plate javascript for inclusion on the client to make the platform a little nicer to use. The result is something like Aptana Studio/Jaxer, which can be pretty nice.

But in the end, there are just too many pitfalls and little compatibility issues to overcome. Ultimately, as expensive as additional servers are they're still cheap compared to additional developers, and most programmers know how to be much more productive using something else.

What I'd really like to see is partial server-side javascript. When a page is requested or a form submitted the server platform does request validation in javascript, perhaps as a plugin to the web server that's completely independent from the rest of it, but the response is built using the platform of your choice.

Joel Coehoorn
Wow! What a typing speed!
Mehrdad Afshari
There is no way you just typed that... :p
Paolo Bergantino
:) The _last_ time someone asked a question like this it was closed just before I could hit submit. I had enough work in the post I decided to save it.
Joel Coehoorn
@Joel - a magician should never reveal his secrets...
Michael Burr
Is there something wrong with this question? why was a similar one closed?
Ronn
Dupe. Why else? I'm a little surprised this stayed open, given the first answer linked to both previous questions.
Joel Coehoorn
Plus: 4. Most web programmers use JavaScript not because they like it, but because nothing else is available! Doing the whole app in that accursed language would be painful.
bobince
Definetly JavaScript is miles better than VBScript with ASP. At least you can use objects.
voyager
+2  A: 

Javascript is just a language. Because it is just a language, you can use it anywhere you want... in your browser, on the server, embedded in other applications, stand-alone applications, etc.

That being said, I don't know that there is a lot of new development happening with "Server-Side Javascript"

Brian Genisio
+2  A: 

Javascript is a perfectly good language with a self / scheme prototype style base and a C style syntax. There are some problems, see Javascript the Good Parts, but in general it's a first rate language. The problem is that most javascript programmers are terrible programmers because it's very accessible to get started.

One team at google built out Rhino on Rails, which is an MVC framework like Ruby on Rails which is written in javascript and runs on Rhino a javascript interpreter for the Java VM. In this case they had a requirement to use the Java VM, but wanted to get a language which was fast (javascript is fast), supported duck typing, and was flexible.

Another example is something like CouchDB, a document oriented database which uses json as it's transport format and javascript as it's query & index language. They wanted the database to be as web native as possible.

Javascript is good at string and dom (xml) manipulation, being sandboxed, networking, extending itself, etc... Those kind of features are the thing you often do when developing web applications.

All that said, i don't actually develop server side javascript. It's not a bad idea, but definitely less common.

rabble
A: 

We use javascript on the client because it is there, not because from a list of languages it was our choice. I wouldn't choose it for any chore on the server.

You can run any language you like on the server, in fact, as many as you like.

javascript is reliable and easy to use, but it is just too labor intensive for common tasks on the server.

kennebec