服务器端Javascript:为什么? [英] Server Side Javascript: Why?

查看:70
本文介绍了服务器端Javascript:为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

服务器端javascript的使用是否普遍?为什么会使用它而不是任何其他服务器端脚本?是否有一个特定的用例使其比其他服务器端语言更好?

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?

另外,对如何开始尝试它感到困惑,我在freeBSD,为了运行服务器端javascript我需要安装什么?

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?

推荐答案

它是这样的:

服务器价格昂贵,但用户可以免费在浏览器中获得处理时间。因此,与任何足以需要运行多个服务器的站点上的客户端代码相比,服务器端代码相对昂贵。但是,有些事情你不能留给客户,比如数据验证和检索。您希望在客户端上执行这些操作,因为这意味着用户的响应时间更短,并且您自己的服务器基础架构更少,但安全性和可访问性问题意味着需要服务器端代码。

Servers are expensive, but users will give you processing time in their browsers for free. Therefore, server-side code is relatively expensive compared to client-side code on any site big enough to need to run more than one server. However, there are some things you can't leave to the client, like data validation and retrieval. 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 server-side code is required.

通常情况是你做两件事。您编写服务器端逻辑是因为必须这样做,但您也在javascript中编写相同的逻辑,希望能够为用户提供更快的响应,并在某些情况下为您的服务器节省一些额外的工作。这对验证代码特别有效。

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

由于我们所有(大多数)程序员都在这里,我们应该立即发现新问题。开发两组相同的逻辑不仅涉及额外的工作,而且还有维护它所涉及的工作,平台产生的不可避免的错误不能很好地匹配,以及随着时间的推移实现漂移而引入的错误。

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 inevitable bugs resulting from platforms don't match up well, and the bugs introduced as the implementations drift apart over time.

输入服务器端javascript。我们的想法是您可以编写一次代码,因此相同的代码可以在服务器和客户端上运行。这似乎解决了大部分问题:您可以同时完成所有服务器和客户端逻辑的完整设置,没有漂移,也没有双重维护。当你的开发人员只需要知道服务器和客户端工作的一种语言时,这也很好。

Enter server-side javascript. The idea is you can write code once, so the same code runs on both server and client. This would appear to solve most of the issue: you get the full set of both server and client logic done all at once, there's no drifting, and 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 four-fold:


  1. 页面的服务器视图仍然与页面的客户端视图非常不同。服务器需要能够执行诸如直接与数据库交谈之类的事情,这些数据库不应该从浏览器完成。浏览器需要做一些事情,比如操作与服务器不匹配的DOM。

  2. 你不能控制客户端的javascript引擎,这意味着仍然会有重要的语言服务器代码与客户端代码之间的差异。

  3. 数据库通常是比Web服务器更大的瓶颈,因此节省的成本很低。

  4. 虽然几乎所有人都知道一点点javascript,没有多少开发人员真正了解和理解javascript 很好

  1. The server view of a page is still very 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 the server.
  2. You don't control the javascript engine of the client, meaning there will still 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.
  4. While just about everyone knows a little javascript, not many developers really know and understand javascript well.

这些都不是完全无法攻击的技术问题:您将服务器支持的语言限制为大多数浏览器都支持的javascript子集,提供了解这个子集和服务器端扩展的IDE,制定一些关于页面结构的规则以最小化DOM问题,并提供一些锅炉板javascript包含在客户端,使平台更好用。结果类似于Aptana Studio / Jaxer,或者最近的Node.js,这可能非常好。

These aren't completely unassailable technical problems: 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, or more recently Node.js, which can be pretty nice.

但并不完美。在我看来,有太多的陷阱和很少的兼容性问题,使这真的很闪耀。最终,与开发人员的时间相比,额外的服务器仍然便宜,并且大多数程序员使用除javascript之外的其他东西都能提高工作效率。

But not perfect. In my opinion, there are just too many pitfalls and little compatibility issues to make this really shine. Ultimately, additional servers are still cheap compared to developer time, and most programmers are able to be much more productive using something other than javascript.

我真的很喜欢看是部分服务器端的JavaScript。当请求页面或提交表单时,服务器平台在javascript中执行请求验证,可能作为Web服务器的插件完全独立于其余部分,但响应使用您选择的平台构建。

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.

这篇关于服务器端Javascript:为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆