Raspberry Piをホームサーバ として使うことを考えて、Node.jsをインストールしてみた。PythonでもRubyでも、何でもよかった。ただ、JavaScriptで動くNode.jsとはどんなものなのか、一度は試してみたかった。
$ sudo apt-get install nodejs
どうやってアプリケーションをつくればいいか分からなかったので、Wikipediaに載っているNode.jsのサンプルを入れてみることにした。
$ nano index.jsプログラムは以下のとおり。
var http = require('http'); http.createServer( function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.end('Hello, world\n'); } ).listen(8000); console.log('Server running at http://localhost:8000/');
ファイルを編集したら、実行してみる。
$ nodejs index.js
このサンプルでは、以下のURLをブラウザに指定してアクセスする。
http://<Raspberry Piのアドレス>:8000/