スポンサーリンク
近頃(だいぶ遅い?)よく聞くnode.jsですが、恥ずかしながらどんなものか全く知りません。
ということで恥をしのんでインストールを行いhello world と表示させるまでの過程をメモしたいと思います。
・javascriptが関係してるらしい
・流行ってるらしい
・簡単にウェブアプリが作れる?らしい??
・Amazon EC2
・Amazon linux
まずはインストールの手順です。2つのコマンドで完結します。
コマンド
sudo su -
curl -sL https://rpm.nodesource.com/setup_11.x | bash -
結果
## Installing the NodeSource Node.js 11.x repo...
## Inspecting system...
+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m
## Confirming "el7-x86_64" is supported...
+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_11.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
## Downloading release setup RPM...
+ mktemp
+ curl -sL -o '/tmp/tmp.Ljjx9CpQO3' 'https://rpm.nodesource.com/pub_11.x/el/7/x86_64/nodesource-release-el7-1.noarch.rpm'
## Installing release setup RPM...
+ rpm -i --nosignature --force '/tmp/tmp.Ljjx9CpQO3'
## Cleaning up...
+ rm -f '/tmp/tmp.Ljjx9CpQO3'
## Checking for existing installations...
+ rpm -qa 'node|npm' | grep -v nodesource
## Run `sudo yum install -y nodejs` to install Node.js 11.x and npm.
## You may also need development tools to build native addons:
sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
sudo yum install yarn
コマンド
sudo yum install -y nodejs
結果
Loaded plugins: priorities, update-motd, upgrade-helper
nodesource | 2.5 kB 00:00:00
nodesource/x86_64/primary_db | 24 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:11.13.0-1nodesource will be installed
--> Processing Dependency: python >= 2.6 for package: 2:nodejs-11.13.0-1nodesource.x86_64
--> Running transaction check
---> Package python26.x86_64 0:2.6.9-2.89.amzn1 will be installed
--> Processing Dependency: libpython2.6.so.1.0()(64bit) for package: python26-2.6.9-2.89.amzn1.x86_64
--> Running transaction check
---> Package python26-libs.x86_64 0:2.6.9-2.89.amzn1 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
========================================================================================================================================================
Package Arch Version Repository Size
========================================================================================================================================================
Installing:
nodejs x86_64 2:11.13.0-1nodesource nodesource 19 M
Installing for dependencies:
python26 x86_64 2.6.9-2.89.amzn1 amzn-main 5.8 M
python26-libs x86_64 2.6.9-2.89.amzn1 amzn-main 697 k
Transaction Summary
========================================================================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 26 M
Installed size: 77 M
Downloading packages:
(1/3): python26-libs-2.6.9-2.89.amzn1.x86_64.rpm | 697 kB 00:00:00
(2/3): python26-2.6.9-2.89.amzn1.x86_64.rpm | 5.8 MB 00:00:00
warning: /var/cache/yum/x86_64/latest/nodesource/packages/nodejs-11.13.0-1nodesource.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID 34fa74dd: NOKEY
Public key for nodejs-11.13.0-1nodesource.x86_64.rpm is not installed
(3/3): nodejs-11.13.0-1nodesource.x86_64.rpm | 19 MB 00:00:00
--------------------------------------------------------------------------------------------------------------------------------------------------------
Total 34 MB/s | 26 MB 00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Importing GPG key 0x34FA74DD:
Userid : "NodeSource <gpg-rpm@nodesource.com>"
Fingerprint: 2e55 207a 95d9 944b 0cc9 3261 5ddb e8d4 34fa 74dd
Package : nodesource-release-el7-1.noarch (installed)
From : /etc/pki/rpm-gpg/NODESOURCE-GPG-SIGNING-KEY-EL
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : python26-libs-2.6.9-2.89.amzn1.x86_64 1/3
Installing : python26-2.6.9-2.89.amzn1.x86_64 2/3
Installing : 2:nodejs-11.13.0-1nodesource.x86_64 3/3
Verifying : 2:nodejs-11.13.0-1nodesource.x86_64 1/3
Verifying : python26-2.6.9-2.89.amzn1.x86_64 2/3
Verifying : python26-libs-2.6.9-2.89.amzn1.x86_64 3/3
Installed:
nodejs.x86_64 2:11.13.0-1nodesource
Dependency Installed:
python26.x86_64 0:2.6.9-2.89.amzn1 python26-libs.x86_64 0:2.6.9-2.89.amzn1
Complete!
以下のコマンドが実行できればインストール成功です。
# node --version
v11.13.0
こちらも割と簡単です。
以下のコードをコピペします。
vim hello.js
↓中身
var http = require('http');
var server = http.createServer(
function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.write('Hello Node JS!!');
response.end();
}
).listen(3000);
次に作成したコードを実行します。
sudo node hello.js
(何も表示されませんが、そのままでOK)
これで準備はOKです。
node.jsはwebサーバとして動作するので、curlコマンドで動作確認します。
curl -D - http://localhost:3000
結果
HTTP/1.1 200 OK
Content-Type: text/plain
Date: Sun, 31 Mar 2019 23:58:44 GMT
Connection: keep-alive
Transfer-Encoding: chunked
Hello Node JS!!
スポンサーリンク