バックエンド開発のためのJavaScript : Javascript for backend development
<h3>バックエンド開発のためのJavaScript : Javascript for backend development</h3>
※本稿は弊社のCTO、Slavi Pantaleevによる記事です。
前回はWEB開発のフロントエンド側のJavaScriptについて、いかに多くのシーンで利用されているかをお話ししました。(2014年WEBの世界で起こっていること(1)(2)(3): The current state of the web)
今回は、JavaScriptのもう一つの使われ方:「WEB開発時のサーバ側」における役割についてお話ししたいと思います。
Node.js
The most important change in the Javascript world that stemmed from Google’s V8 engine is the appearance of the Node.js platform in 2009. Node.js is an environment for running Javascript on your server. The Node.js platform is a combination of Google’s V8 Javascript engine and a set of APIs (ways to do networking, filesystem operations, process management, etc.) that help with the development of server-side programs. Node.js enables developers to use Javascript to write server-side (backend) programs. Not only that, but it’s highly-performant, scalable and ideal for creating distributed network services. The Node.js environment functions in a similar way to how Javascript works in the browser. Programs are simple to write and understand. This allows frontend web developers that already know Javascript to easily transition into writing backend code.Node.js
GoogleのV8エンジン由来のJavaScriptの世界において最も重要な変化といえば、2009年のNode.jsプラットフォームの登場です。
Node.jsはサーバ上でJavaScriptを動作させる環境です。Node.jsプラットフォームは、GoogleのV8 JavaScriptエンジンといくつかのAPI(ネットワーキングを行う手段、ファイルシステム操作、プロセス管理など)の組み合わせでできており、サーバ側のプログラムの生成を助けます。
開発者がJavaScriptでサーバ側(バックエンド)のプログラムを記述できるのは、Node.jsがあるからです。それだけでなく、高性能で拡張性があり分散型ネットワーク·サービスを作成すには最適です。
Node.jsの環境機能はJavaScriptがブラウザ内で動作するのに似ています。プログラムはシンプルで理解しやすく、書きやすい。すでにフロントエンドでJavaScriptを知っているWEB開発者なら、バックエンドのコードに遷移する書き込みが簡単にできます。
If you are to write a real-time chat system (like Facebook’s chat or Omegle) in a language like PHP (or Ruby or Python), you will face many challenges. To make it real-time, you need to push messages from the server to your users as fast as possible. There are 2 ways to do that:
-
frequent polling – you make the user’s browser ask your server “are there new messages for me” often (multiple times a second)
-
comet-streaming (long-polling) – you keep an open connection between the user’s browser and your server at all times and push new message over it (as soon as your server is made aware that there are new messages)
Both approaches don’t work well under scale.
Frequent polling leads to many re-connections and a lot of overhead – each request takes significant time to process.
Long-polling wastes a lot of memory – each open connection takes some megabytes of memory – you can’t service many users with a single server.
The modern web requires that you can handle tens of thousands of concurrent users at the same time. This is also known as the c10k problem.
Node.js is designed to be great at handling a lot of concurrent users and their data in real-time. It can keep thousands of client connections open and periodically do small amounts of work for each one.
(For completeness, we need to mention that there are ways to achieve the same result in other languages, albeit in a more complicated way compared to Node).
小規模WEBサービスに適したNode
Node.jsは、小型かつ便利なバックエンドプログラムを作成するのにも使用できます。
なんでもこなせる本格的なサーバの必要すらありません。シンプルなAPIサーバか、あるいはサービス指向型アーキテクチャの一部分の小さなコンポーネントで事足ります。
(訳注:service-oriented architecture:ビジネス・プロセスの業務機能サービスに基づいたシステム設計構造◆【略】SOA(出典:アルク辞書))
Node’sの能力を活用するのに、他のコードの使用をあきらめる必要はありません。現代のWEB開発は多様です – 多くの言語、フレームワーク、それぞれに複数の利点があります。あなたがテクノロジーのミックスを恐れなければ(そしてまたそうであるべきなのですが)、ほとんどのコードについてはあなたお気に入りのWEB開発言語が使えるでしょう、そして理にかなっている部分のみにNodeを使用すればよいのです。
Comments are closed.