views:

406

answers:

7

I'm now thinking to establish my server-side code in JavaScript, and begin to do all on it, but I want to know about its security and flexibility compared to PHP.

I want to know too, if it can be successfully used to develop things like forum boards, full web-sites and things like this, as PHP does.

+1  A: 

In a word: no. Javascript is a client-side language. In order to do the things that you are describing, you need a server-side language such as PHP.

EDIT: OK, technically it is possible to implement Javascript in other areas besides the browser, but this is not very common.

Jeff Fohl
Javascript is not a client-side language. It's a language which is mostly deployed client-side. **Wow** that's a big distinction. I use Javascript server-side all the time.
T.J. Crowder
@T.J. Crowder: you could have given a suggestion, javascript is deployed client side boils down to same, no down vote for that at least but there are people too fast too furious and go on down voting and also close the legitimate questions.
Sarfraz
Downvote switched to upvote due to edit. Thank you for taking the time to examine comments and modify your answer accordingly :)
Matchu
@Sarfraz: I did make suggestions. And I did the last time you called it a client-side language.
T.J. Crowder
+7  A: 

Javascript is just now starting to get some presence on the server, with things like ServerJS and nodeJS, but right now, you would probably be best off using PHP for your server side code, and javascript for client-side beautification.

W_P
+1. especially NodeJS is freaking fast due to V8.
back2dos
Aww, does no-one remember Netscape Enterprise Server and its take on server-side JavaScript? It was ahead of its time! And quite horrible, of course.
bobince
@bobince: Oh, I remember (see my reply to Sarfraz on my answer).
T.J. Crowder
I'll add http://ringojs.org to the mix - built by people who do serverside JS since way back.
oberhamsi
+4  A: 

The way they are usually used, PHP and JavaScript run in entirely different worlds, and are not really comparable. (There is a server-side version of JavaScript but it's fair to say it's not especially widespread yet, and doesn't run on standard web hosting.)

The security issues you are going to encounter in JavaScript (on the browser) side are very different from what you have to look out for in PHP.

I want to know too, if it can be sucessfully used to develop things like forum boards, full web-sites and things like this, as PHP does.

No, not with client-side Javascript. For dynamic applications, you will always need some server-side language backing it, be it PHP or some other language like ASP, Python, Ruby, Perl....

Pekka
Re *"There is a server-side version of JavaScript but it's fair to say it's not especially widespread yet, and doesn't run on standard web hosting."* IIS is an **incredibly** standard, widespread web hosting platform, and has supported Javascript for, oh, a decade or so. Another very widespread platform is any JVM hosting solution, for which you can write Javascript with Java. It's not *quite* as widespread as PHP yet, but running on IIS and any JVM solution makes it very widely-available and supported indeed.
T.J. Crowder
@T.J. Crowder: But the point is, how many hosts do you know of that support server-side JavaScript right off the bat? Also, there is no standard for server-side JavaScript, so each implementation uses its own libraries and APIs, which is annoying.
musicfreak
@musicfreak: My point is that a lot of people have hosting packages *right now* that support Javascript and they don't even know it. Have an IIS-based hosting package? It supports Javascript on the server, right off the bat. Have a hosting package on Tomcat, Resin, or anything else with a JVM? It supports Javascript, right off the bat. Between those two, I'd say we're looking at more than half the hosting providers out there (because although Apache **dominates** the market, if you look, the vast majority of companies providing Apache hosting *also* provide IIS or JVM hosting).
T.J. Crowder
@musicfreak: (cont'd) There's still a lot of room for improvement, but that's where the CommonJS project (speaking of standards), the v8cgi project (Google's V8 engine as a FastCGI module for Apache), etc. come in. So right now, today, you can use everything Classic ASP has, and everything Java on the server has. (And heck, if you count JScript.Net, IIS supports "Javascript" with access to everything .Net has.) And the future's bright. :-)
T.J. Crowder
i would downvote for not answering the question but can't.JavaScript on the server is not supported by many hosting companies but conceptually not different from hosting PHP, python and what not.
oberhamsi
@oberhamsi if you look at the original revision of the question, you will see that it's a bit unclear whether the OP knows about server-side Javascript at all. I still think what he meant was client-side JS. Whichever way, the server side aspect has been answered fine since in other answers so I don't really see what the problem is
Pekka
okay, no problem but this seems to clearly be about serverside JS as the questions says now: "I'm now thinking to establish my server-side code in JavaScript"
oberhamsi
@oberhamsi it was edited into that form by somebody else. Whether it is what the OP really meant, we do not know. Also, the accepted answer deals with server side JS so I don't see the problem
Pekka
A: 

PHP and JavaScript are two different languages that do two different things. One cannot replace the other. You are most likely going to use a combination of the two. JavaScript for client-side stuff. PHP for server-side stuff.

xanadont
+2  A: 
T.J. Crowder
@T.J. Crowder: You should see Client-side-javascript: http://en.wikipedia.org/wiki/Client-side_JavaScript
Sarfraz
@Sarfraz: Oh absolutely, Javascript's had most of its success client-side, there's no doubt about it. But to call it a "client-side language" is to think very narrowly indeed (and to be unaware of the language's history; it's been used on servers *since it was created*).
T.J. Crowder
+2  A: 

To replace PHP with Javascript, you need server-side Javascript and there is a lot happening on that front. Mozilla’s Rhino runs Javascript atop the JVM and it seems Google is also working on its own server side Javascript framework. The most popular in-production implementations are:

  • Helma: Several active projects are using it, runs on Jetty & Rhino and lets developers leverage the power of JVM, has its own object-oriented MVC framework
  • Project Phobos: runs on Glassfish & Rhino and lets developers leverage the power of JVM, includes plug-ins for NetBeans and integrates with jMaki Web UI framework
  • JSSP: A very simple server side framework, a lot like classic ASP, JSP and PHP

Aptana’s Jaxer showed a lot of promise, especially by bring the DOM to server side, but the project seems dead now. From what I understand, node.js is not a server-side Javascript framework in the same sense as Helma and Phobos. Instead it can be used for writing event-driven servers in Javascript (sort of like writing your own application server).

Abbas
@Abbas: +1 for great details and those links :)
Sarfraz
I think development on Google's JavaScript framework had stopped, because that post is 3 years old and there has been no new news on it. Either they're keeping it secret or they aren't working on it anymore.
musicfreak
A: 

This is slightly off-topic, but it may actually get to the core of your question:
if you want to use only one language for web applications, you may wanna have a look at haXe.

It is a cross-platform language, that (among other targets) compiles to JavaScript and PHP source as well as NekoVM bytecode. For server-side JavaScript, there are NodeJS bindings.

This way you are not bound to a specific platform. The neko and PHP APIs are largely compatible, so you can deploy on both platforms, having the option to choose neko's speed and persistency or PHP's ease of deployment. Please note however, the PHP output has a little overhead although common optimizers as eaccelerator will make this barely noticeable.

haXe is significantly less forgiving than both JavaScript and PHP. This makes it harder to learn, but a much safer, robust and in the end more productive tool.

greetz
back2dos

back2dos