Nathan Campos
Nathan Campos

Reputation: 29497

JavaScript on the server-side like PHP

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.

Upvotes: 2

Views: 6521

Answers (8)

back2dos
back2dos

Reputation: 15623

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.

Upvotes: 0

Jeff Fohl
Jeff Fohl

Reputation: 2076

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.

5 YEAR EDIT: Well, 5 years later, this answer obviously is not accurate, with the popularity of things like node.js. Let that be a testament to how quickly things can change!

Upvotes: -1

Kris Zhang
Kris Zhang

Reputation: 317

Yes, my site is written by node.js

Using websvr, it's Java style have filter and handlers, hosting on debian OS.

Upvotes: 0

Abbas
Abbas

Reputation: 6886

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 bringing the DOM to the 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 (for example: writing your own web server).

Upvotes: 2

T.J. Crowder
T.J. Crowder

Reputation: 1074295

The question is very, very broad. Interpreting it as "can I use Javascript on the server":

Fundamentally, sure, Javascript is a very powerful language and so you can do development in it server-side just like you can client-side (and if you do client-side scripting as well, you get some definite reuse benefits using Javascript on the server).

  • For Apache systems, there's the v8cgi project (a FastCGI Javascript plug-in with connectors, using Google's freaky-fast V8 engine).
  • On Microsoft-based systems, IIS supports Javascript (JScript) on the server out of the box (I use that all the time), which has access to all of the ActiveX stuff (e.g., for talking to databases, dealing with the file system, etc.).
  • If your server framework is JVM-based, there's Rhino, which is Javascript for the Java platform and has access to all (or nearly all) of the libraries available for Java — e.g., a huge ecosystem of libraries and plug-ins.
  • Aside from v8cgi, there are a couple of other projects built on Google's V8 engine.
  • There's a place that does a full stack for you called chromeserver (I don't know what their backend is; I'm not going to infer from the name).
  • Paul mentioned ServerJS and NodeJS.
  • There's the whole CommonJS project.

Etc. etc. etc. There's quite a list on Wikipedia.

Arguing against, there's a very rich ecosystem built around PHP. Unless you're using something like Rhino for the Java platform or JScript on IIS (because of the ecosystems they leverage), you may find that you don't have nearly that ecosystem available to you when developing in Javascript for the server. I mean, if you're looking for pre-built forum or wiki software (for example), let's just say you can't swing a dead cat without finding one based on PHP, and the same cannot be said of Javascript on the server.

Upvotes: 6

Pekka
Pekka

Reputation: 449435

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....

Upvotes: 5

xanadont
xanadont

Reputation: 7604

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.

Upvotes: -4

user35288
user35288

Reputation:

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.

Upvotes: 9

Related Questions