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

查看:40
本文介绍了服务器端 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 中编写相同的逻辑,以期为用户提供更快的响应并在某些情况下为您的服务器节省一些额外的工作.这对验证码特别有效;浏览器中的验证检查失败可以在服务器上保存整个 http 请求/响应对.

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; a failed validation check in a browser can save an entire http request/response pair on the server.

由于我们都是(大部分)程序员,我们应该立即发现新问题.不仅开发两组相同的逻辑涉及额外的工作,而且维护它所涉及的工作,平台导致的不可避免的错误不能很好地匹配,以及随着实现的时间推移而引入的错误.

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. 数据库通常是比网络服务器更大的瓶颈,因此节省的成本和性能优势最终会低于预期.
  4. 虽然几乎每个人都知道一点点 javascript,但真正了解和理解 javascript 的开发者并不多很好.

这些并非完全无懈可击的技术问题:您将服务器支持的语言限制为大多数浏览器都支持的 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 服务器的插件,完全独立于其余部分,但 响应em> 是使用您选择的平台构建的.

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天全站免登陆