LoginSignup
38

More than 5 years have passed since last update.

Vagrant上のCentOSにNode.jsをインストール

Last updated at Posted at 2014-09-15

http://nodejs.jp/
http://nodejs.org/

Node.jsっていまいち意味がわからないんですよね。
ESとかv8ってなんだよ。

Node.jsは高速でスケーラブルなネットワークアプリケーションを 簡単に構築するためにChrome の JavaScript 実行環境 上に構築されたプラットフォームです。

Node.js はイベント化された入出力を扱うUnix系プラットフォーム上のサーバーサイドJavaScript環境である

日本語でおk

とりあえずVagrantを起動。

nodebrewをインストール

https://github.com/hokaccha/nodebrew
nodebrewはNode.js専用のバージョン管理ツールらしい。
ヘルプに従ってインストール。

$ cd .
$ curl -L git.io/nodebrew | perl - setup

> Bareword found where operator expected at - line 3, near "400 Bad" (Missing operator before Bad?)
> Bareword found where operator expected at - line 6, near "<p>Your" (Missing operator before Your?)
> syntax error at - line 2, near "html>"
> Execution of - aborted due to compilation errors.

あれ?
何故かワンライナーが使えないので手動でゲット

$ wget git.io/nodebrew
$ perl nodebrew setup

> install nodebrew in $HOME/.nodebrew
> Add path: export PATH=$HOME/.nodebrew/current/bin:$PATH

インストールされたバージョンは0.7.4だった。

nodebrewパスの追加

インストールの最後にexportしろやと言われたので実行。

$ export PATH=$HOME/.nodebrew/current/bin:$PATH

ついでに.bashrcにも入れとく。

$ vi .bashrc

export PATH=$HOME/.nodebrew/current/bin:$PATHを追加。

$ source ~/.bashrc
$ nodebrew help

コマンドが見つかりませんと言われなくなったら成功。

Node.jsをインストール

全てnodebrewにお任せ。

# nodebrew自身を更新
$ nodebrew selfupdate
# 全バージョンを確認
$ nodebrew ls-remote
# 最新安定版をインストールする。コンパイルが遅い。
$ nodebrew install stable
# 最新安定版のコンパイル済のを入れてくれるらしい。早い。
$ nodebrew install-binary stable
# 使用するバージョンを選択
$ nodebrew use v0.10.29
# インストールされているバージョンを確認。currentが現在有効なバージョン。
$ nodebrew ls

install-binary stableした。

実行時点での最新安定版はv0.10.29。

動作確認。

$ node -v
> v0.10.29

インストールできた。
ここまで管理者権限を一切使ってないのが不思議。

JavaScriptファイルを作成

適当な場所にexample.jsを作成。
特にDocumentRoot以下とかでなくてもいい。
というかNode.jsはApacheやnginxと同等のWebサーバ的なものだったようだ。
ここまで進めてきて初めて知る真実。

中身は基礎から学ぶNode.jsの流用。

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, "192.168.33.10");
console.log('Server running');

極普通のJavaScriptである。
起動。

$ node path/to/example.js
> Server running

Node.jsが起動して192.168.33.10:1337を待ち受けるようになった。
簡単。
何故1337かは不明。

VirtualBox内のゲストOSのブラウザから192.168.33.10:1337にアクセス。

Hello World

正常に動作した。

ホストOSのWindowsから192.168.33.10:1337にアクセス。

このウェブページにアクセスできません

あれ?

iptables編集

# service iptables stop

Windowsから192.168.33.10:1337にアクセス。

Hello World

行けた。
iptablesで1337番ポートが塞がれていたせいだった。
単にオフにするとセキュリティ的に困るので、必要なポートだけ空ける。

# iptables -I INPUT 7 -p tcp --dport 1337 -j ACCEPT
# service iptables save
# service iptables restart

"7"は環境によって違う可能性があり、REJECTと書いてある行を指定する。
REJECTの直前にACCEPT行が挿入され、ポート1337が使えるようになった。

おわりに

これでサーバサイドJavaScriptが書けるようになった。
が、特段JavaScriptが得意ってわけでもないし特にやりたいこともなかったので何をやったらいいのかわからない。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
38