Node.jsの遊び場構築

Node.jsを触ってみたい、その一心。

【今回お世話になったサイト群】

いまアツいJavaScript!ゼロから始めるNode.js入門〜5分で環境構築編〜

creams@nexus: Node.js をnvmで管理、foreverインストールで問題?

Node.jsのバージョン管理をしてくれるnvmとやらがいるらしい。 早速導入してみる。

# git clone https://github.com/creationix/nvm.git ~/.nvm
# source ~/.nvm/nvm.sh

Node.jsのバージョンリストアップ

# nvm ls-remote
        v0.1.14
        v0.1.15
        v0.1.16
    ...(中略)...
        v0.12.5
        v0.12.6
        v0.12.7
    iojs-v1.0.0
    iojs-v1.0.1
    iojs-v1.0.2
    ...(中略)...
    iojs-v2.3.4
    iojs-v2.4.0
    iojs-v2.5.0

インストールと確認

# nvm install 2.5.0
# node -v
v2.5.0

デフォルトで使用するバージョンの指定

#  nvm alias default v2.5.0
default -> v2.5.0 (-> iojs-v2.5.0)

うむ、とりあえずこれでNode.jsが入ったらしい。

手順に沿ってexample.jsを作っとく。

var http = require('http');
 
http.createServer(function (request, response) {
  response.writeHead(200, {'Content-Type': 'text/plain'});
  response.end('Hello World\n');
}).listen(8124);
 
console.log('Server running at http://127.0.0.1:8124/');

よし、機は熟せり! 試しに起動してみる。

# node example.js
Server running at http://127.0.0.1:8124/

接続してみようとブラウザも開くも、アクセスできず。 そうか、そりゃそうだな。。。そんなポート開いてないもの。。。 nginx中継してリバースプロキシで接続することにしよう。

nginxの設定にリバースプロキシの設定を行う。

  location ^~ /nodejs {
    proxy_pass http://localhost:8124/;
  }

とりあえずドメイン+/nodejsへのアクセスをnodejs側に飛ばす設定に。

このままじゃコンソール落としたらアプリも落ちるので大変不便。 なので永続的にNode.jsを起動した状態にできるようにforeverとやらを導入する。

# npm install forerver -g
npm ERR! Linux 2.6.32-431.29.2.el6.x86_64
npm ERR! argv "/root/.nvm/versions/io.js/v2.5.0/bin/iojs" "/root/.nvm/versions/io.js/v2.5.0/bin/npm" "install" "forerver" "-g"
npm ERR! node v2.5.0
npm ERR! npm v2.13.2
npm ERR! code E404

npm ERR! 404 Not Found: forerver
npm ERR! 404
npm ERR! 404 'forerver' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! Please include the following file with any support request:
npm ERR! /etc/nginx/conf.d/npm-debug.log

creams@nexus: Node.js をnvmで管理、foreverインストールで問題?

こちらで記載のあるように、確かにエラーが出た。 右へならえで、以下のコマンドを試す。

# npm install forever -g -f

とりあえずインストールには成功した模様。

※ここまでの記事と、ここから先の記事の間に、数日間経過しています。 以降の記事を書く前に、作業の振り返りをしてたらnodejsのver3.0.0が出てたので それをインストールしなおしてから以降の作業を実施しております。

# forever
help: usage: forever [action] [options] SCRIPT [script-options]
help:
help: Monitors the script specified in the current process or as a daemon
(中略)
help: without using nohup. It is recommended to run start with -o -l, & -e.
help: ex. forever start -l forever.log -o out.log -e err.log my-daemon.js
help: forever stop my-daemon.js

うむ、大丈夫そう。

ではさっきのアプリを永続的に起動させてみる。

# forever start example.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: example.js

なんか警告出てるけど起動できたっぽい? 確認してみる。

# forever list
info: Forever processes running
data: uid command script forever pid id logfile uptime
data: [0] P-8I /root/.nvm/versions/io.js/v3.0.0/bin/iojs example.js 18398 18403 /root/.forever/P-8I.log 0:0:5:3.152

ほぅ。

ブラウザからも大丈夫そう。

とりあえず遊べるだけの環境は整えることが出来た気がするので終了!