サーバサイド JavaScript 環境として使える Node.js を試してみた。
Node.jsは高速でスケーラブルなネットワークアプリケーションを 簡単に構築するためにChrome の JavaScript 実行環境 上に構築されたプラットフォームです。 Node.jsはイベント駆動とノンブロッキング I/O モデルを使用することにより 軽量・効率的で、分散されたデバイスにまたがるデータ集約的なリアルタイム アプリケーションに最適です。
node.js
今回の環境
Debian GNU/Linux wheezy
$ uname -mrsv
Linux 3.2.0-4-amd64 #1 SMP Debian 3.2.65-1+deb7u1 x86_64
$ cat /etc/debian_version
7.8
Node.js をインストールする
# aptitude install nodejs
以下の新規パッケージがインストールされます:
libc-ares2{a} libv8-3.14.5{a} nodejs
Debian では node コマンドではなく、 nodejs コマンドを使うらしい。
$ nodejs -v
v0.10.29
シンプルなサンプル hello, world
ソースコード。
$ cat ./hello.js
var message = "hello, world";
console.log(message);
実行結果。
$ nodejs ./hello.js
hello, world
シンプルな Web サーバを動かす
ソースコード。
$ cat ./webserver.js
var http = require('http');
var server = http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<html><body>hello, world</body></html>');
});
server.listen(8080);
console.log('server started');
実行。
$ nodejs ./webserver.js
server started
Web ブラウザで http://localhost:8080/ などにアクセスすると「hello, world」と表示される。
参考資料
追記: 2015-03-01
Debian の nodejs パッケージは 現在の安定版である wheezy には入っていなくて、 backports の中にある (次期バージョンの jessie には nodejs パッケージは存在する)。
Package: nodejs (0.10.29~dfsg-1~bpo70+1)
Debian -- Details of package nodejs in wheezy-backports
そのため、 /etc/apt/sources.list に以下のような項目が無いと nodejs パッケージをインストールできない。
deb http://cdn.debian.net/debian/ wheezy-backports main contrib non-free
ref. [ヅ] Debian GNU/Linux 7 wheezy での /etc/apt/sources.list の設定と日々のセキュリティアップデート (2014-04-11)
tags: node.js javascript
Posted by NI-Lab. (@nilab)