5

我现在正在考虑用 JavaScript 建立我的服务器端代码,并开始在上面做所有事情,但我想知道它与 PHP 相比的安全性和灵活性。

我也想知道,它是否可以像 PHP 那样成功地用于开发论坛板、完整的网站和类似的东西。

4

8 回答 8

9

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.

于 2010-04-18T20:57:58.127 回答
6

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.

于 2010-04-18T20:59:46.903 回答
5

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

于 2010-04-18T20:58:17.923 回答
2

要将 PHP 替换为 Javascript,您需要服务器端 Javascript,并且在这方面发生了很多事情。Mozilla 的Rhino在 JVM 上运行 Javascript,而且 Google 似乎也在开发自己的服务器端 Javascript 框架。最流行的生产实现是:

  • Helma:几个活跃的项目正在使用它,在 Jetty 和 Rhino 上运行,让开发人员利用 JVM 的力量,拥有自己的面向对象的 MVC 框架
  • Project Phobos:在 Glassfish 和 Rhino 上运行,让开发人员利用 JVM 的强大功能,包括 NetBeans 插件并与 jMaki Web UI 框架集成
  • JSSP:一个非常简单的服务器端框架,很像经典的 ASP、JSP 和 PHP

Aptana 的 Jaxer 表现出了很大的希望,尤其是通过将 DOM 带到服务器端,但该项目现在似乎已经死了。据我了解,node.js 不是与 Helma 和 Phobos 相同的服务器端 Javascript 框架。相反,它可以用于用 Javascript 编写事件驱动的服务器(例如:编写自己的 Web 服务器)。

于 2010-04-18T21:17:24.077 回答
0

是的,我的网站是由 node.js 编写的

使用websvr,它的 Java 风格有过滤器和处理程序,托管在 debian 操作系统上。

于 2013-06-07T02:51:14.690 回答
0

这有点离题,但它实际上可能触及您问题的核心:
如果您只想将一种语言用于 Web 应用程序,您可能想看看Haxe

它是一种跨平台语言,(在其他目标中)编译为 JavaScript 和 PHP 源代码以及 NekoVM 字节码。对于服务器端 JavaScript,有 NodeJS 绑定。

这样,您就不会绑定到特定平台。neko 和 PHP API 在很大程度上兼容,因此您可以在两个平台上进行部署,可以选择 neko 的速度和持久性或 PHP 的易于部署。但是请注意,PHP 输出有一点开销,尽管常见的优化器作为加速器会使这几乎不引人注意。

Haxe 的宽容度明显低于 JavaScript 和 PHP。这使得它更难学习,但却是一个更安全、更强大且最终更高效的工具。

于 2010-04-19T00:19:35.977 回答
-1

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!

于 2010-04-18T20:57:54.100 回答
-4

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.

于 2010-04-18T20:58:51.547 回答