https://nodejs.org/download/ よりダウンロード
インストール後シェルから
# node -v v0.12.2
インストールされていることが確認できました。*1
MacとかだとHomebrewなどがいいんですかね。
# brew install node
もしくは nodebrewで。 node.jsのversionを管理するためにnodebrewを利用する - Qiita
# cat << __EOF__ >> helloworld.js var http = require('http'); var server = http.createServer( function (request, response) { response.writeHead(200, {'Content-Type': 'text/plain'}); response.write('Hello World!!\n'); response.end(); } ).listen(9000); console.log('Server running at http://127.0.0.1:9000/'); __EOF__ # node helloworld.js 6 Apr 23:39:07 - Server running at http://127.0.0.1:9000/
ブラウザやcurlで、 http://localhost:9000/ へアクセスすると、*2 予想通り、
Hello World!!
が表示されました。
簡単にWebサーバが構築できましたねー。
とりあえず旧版をインストールして、最新版に入れ替えます。
$ sudo apt-get install -y nodejs npm $ node --version v0.10.29 $ sudo npm cache clean $ sudo npm install n -g /usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n n@2.1.4 /usr/local/lib/node_modules/n $ sudo n stable .... installed : v7.2.0 $ sudo n 7.2.0 $ node --version v7.2.0 $ sudo n 6.9.1 $ node --version v6.9.1