Node.js
![]() | |
作者 | ライアン・ダール |
---|---|
開発元 | Node.js Developers |
初版 | 2009年 |
リポジトリ | |
プログラミング 言語 | C++, JavaScript |
対応OS | macOS, Linux, Solaris, FreeBSD, OpenBSD, Windows, webOS |
プラットフォーム | x86, x64, ARM, Power, z/Architecture |
種別 | イベント駆動型 |
ライセンス | MIT License |
公式サイト | nodejs.org |
Node.js はV8 JavaScriptエンジン上に構築されたJavaScript実行環境の1つである[1]。イベント化された入出力を扱うサーバーサイドJavaScript環境であり、Webサーバなどのスケーラブルなネットワークプログラムの記述を意図している[2]。ライアン・ダールによって2009年に作成され、ダールを雇用しているJoyentの支援により成長している[3] [4]。
概要
V8 JavaScriptエンジンで動作するが、ChakraCoreバージョンやMozillaによるSpiderMonkey移植のプロジェクトも存在する。
Node.jsはPythonのTwisted、PerlのPerl Object Environment、C言語のlibevent、RubyのEventMachineと同様の目的を持つ。 ほとんどのJavaScriptとは異なり、ウェブブラウザの中で実行されるのではなく、むしろサーバサイドJavaScriptの一種である。 Node.jsはいくつかのCommonJS仕様を実装している[5]。 Node.jsは対話的なテスト用にREPL (Read-eval-print loop) 環境を含んでいる。
Node.jsを用いた構成としてはMEAN等が提唱されている。
例
Node.jsによるHTTPサーバ版のHello world:
const http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://127.0.0.1:3000/');
ポート番号 3000番で接続を待ち受けて、送信されたデータをエコーする単純なTCPサーバ:
const net = require('net');
const server = net.createServer(function (stream) {
stream.write('hello\r\n');
stream.on('data', function (data) {
stream.write(data);
});
stream.on('end', function () {
stream.end('goodbye\r\n');
});
});
server.listen(3000, 'localhost');
モジュール
Node.jsは、バイナリにコンパイルされた多くの「コア・モジュール」とともに提供される。それはネットワークの非同期ラッパーであるnetモジュールの他、パスやファイルシステム、バッファ、タイマー、より一般的なストリームなどの基本的なモジュールを含む。サードパーティー製のモジュールを使用することも可能である。それはプリコンパイルされた ".node" アドオン、または、プレーンなJavaScriptファイルのどちらの形式でもよい。JavaScriptモジュールはCommonJSモジュール仕様[6]に従って実装され、モジュールが実装する関数や変数へのアクセスにはexports変数が使われる[7]。
サードパーティーのモジュールはNode.jsを拡張または抽象レベルを提供することで、ウェブアプリケーションで使われる様々なミドルウェア実装することができる。たとえばポピュラーなフレームワークとしてconnectおよびExpress.jsがある。モジュールは単なるファイルとしてインストールすることもできるが、通常はnpmを使ってインストールされる。それは依存性の扱いも含めてモジュールの構築、インストール、更新を助けてくれる。さらに、モジュールはNodeのデフォルトであるモジュール用ディレクトリにインストールしなくても、相対的なパス名を要求することで見つけられる。Node.js wikiに利用可能なサードパーティー製のモジュール一覧がある。
Node.jsを用いたWebアプリケーションでは、Express.jsやEmber.js、Matadorのようなフレームワークを用いて開発を行うことが多い。
表明
Node.jsはコアモジュール assert
により表明(assertion)に対応している。表明違反時にはassert.AssertionError
インスタンスがスローされる[8]。
const assert = require('assert').strict;
assert.deepEqual(1, 2);
// Thrown:
// AssertionError [ERR_ASSERTION]: Expected values to be strictly deep-equal:
//
// 1 !== 2
リリース
Node.jsは長期サポート(LTS)リリースモデルを採用している。
各メジャーバージョンはPending -> Current -> (Active TLS -> Maintenance TLS ->) End of Life のリリース状態を経る。Currentリリースは6ヶ月間続き、奇数バージョンはその後LTSを経ることなくサポートが終了し、偶数バージョンはLTSへ移行する。プロダクションアプリケーションは Active TLS あるいは Maintenance TLS のいずれかのみを利用しなければならない[9]。
コミュニティ
主に2つのメーリングリスト nodejs と nodejs-dev 、そして freenode上の IRC チャンネル #node.js を中心とするとても活発な開発者コミュニティが存在する。コミュニティはNode.jsにフォーカスした開発者会議であるNodeConfに集結する[10]。
Windows版
0.5.1より、Windowsネイティブ版バイナリをリリースをした[11]。Windowsネイティブ版リリースに関してはマイクロソフトの支援が行われた。Windows向けの非同期I/O環境(Input/output completion port、以下IOCP)に対応するため、libuv[12]を作成することにより抽象化を進めた。結果としてlibev、libeioが使えるUnix系プラットフォームとIOCPを利用するWindowsプラットフォーム向けのリリースが可能になった。
クライアント1万台問題
非同期処理のNode.jsではクライアント1万台問題は起きない[13]。
Node.jsでこの問題を解決した技術の中核は、シングルスレッドにおける非同期処理を容易に実装可能にしたイベント駆動型プログラミング環境である。
Docker Image
Node.js公式からDockerイメージが配布されている[14]。イメージは以下の3種類に大別され、全てのイメージでnode
/npm
/yarn
がプリインストールされている[15]。
node:<version>
: デファクトスタンダード[16]。Docker公式のbuildpack-deps
を基に構築[17]node:alpine
: Alpine Linuxベース[18]。イメージサイズを最小化したい際に推奨される[19]node:slim
: Node.js動作に必要最低限のパッケージのみを含む[20]。サイズに制限がない限りデファクトイメージの利用を強く推奨[21]
デフォルト以外の設定(例: npmアップグレード)を利用する手引き「Docker and Node.js Best Practices」や GetStarted が公式から提供されている。
脚注・出典
- ^ Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js
- ^ http://www.readwriteweb.com/hack/2011/01/wait-whats-nodejs-good-for-aga.php
- ^ http://mashable.com/2011/03/10/node-js/
- ^ Alex Handy (2011年6月24日). “Node.js pushes JavaScript to the server-side”. SDTimes. 2011年6月24日閲覧。
- ^ http://wiki.commonjs.org/wiki/Implementations/node.js
- ^ “CommonJS Implementations”. 2011年5月15日閲覧。
- ^ Ryswyck, Jan. “Taking Baby Steps with Node.js – CommonJS and Creating Custom Modules”. 2011年5月15日閲覧。
- ^ All errors thrown by the
assert
module will be instances of theAssertionError
class. Node.js - ^ Production applications should only use Active LTS or Maintenance LTS releases. Node.js
- ^ http://www.readwriteweb.com/hack/2011/04/nodeconf-schedule-announced.php
- ^ http://blog.nodejs.org/2011/07/14/node-v0-5-1/
- ^ https://github.com/joyent/libuv
- ^ 福田崇男 (2012年7月31日). “スタバ方式で「C10K問題」を解消”. 日経コンピュータ. 日経BP社. 2017年2月20日閲覧。
- ^ The official Node.js docker image, made with love by the node community. [1]
- ^ All of the images contain pre-installed versions of
node
,npm
, andyarn
. [2] - ^ This is the defacto image. [3]
- ^ This tag is based off of
buildpack-deps
. [4] - ^ This image is based on the popular Alpine Linux project, available in the
alpine
official image. [5] - ^ This variant is highly recommended when final image size being as small as possible is desired. [6]
- ^ This image does not contain the common packages contained in the default tag and only contains the minimal packages needed to run
node
. [7] - ^ Unless you are working in an environment where only the Node.js image will be deployed and you have space constraints, we highly recommend using the default image of this repository. [8]