1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

CentOS 7.2にnode.jsをインストールする

Posted at

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
1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?