CentOS 7.2にnode.jsをインストールしたときのメモ
node.jsインストール
$ cd ~/git/github/
$ mkdir creationix
$ cd creationix
$ git clone git://github.com/creationix/nvm.github
$ echo . ~/git/github/creationix/nvm/nvm.sh >> ~/.bashrc
$ . ~/.bashrc
$ nvm install stable
$ node -v
v7.1.0
$ npm -v
3.10.9
サンプルプログラム作成
$ cd ~/workspace
$ mkdir nodejs && cd nodejs
$ npm init
$ emacs index.js
node.js
var http = require('http');
var server = http.createServer();
server.on('request', doRequest);
server.listen(8080);
console.log('Server running');
function doRequest(req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write('Hello World¥n');
res.end();
}
$ node index.js
ポート制限
環境によってはポート8080など、80(HTTP)、443(HTTPS)以外は閉じられていることがある。サーバ側で対応する場合は以下。
$ emacs myiptables.sh
iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080
iptables -t nat -I PREROUTING -p tcp --dport 443 -j REDIRECT --to-ports 8443
$ sudo chmod u+x myiptables.sh
$ sudo sh myiptables.sh