Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8934

【AWS】EC2インスタンスにnode.jsをインストールしてサンプルプログラムを実行

$
0
0

環境

Windows 10 Home
Tera Term 4.105(Macの人はTerminal)

前提

・Amazon Linux EC2
 -SSH接続可能
 -アウトバウンドのHTTP接続が可能

node.jsをインストール

ec2-linux
$ cd ~
$ wget https://nodejs.org/dist/v12.18.2/node-v12.18.2-linux-x64.tar.xz

公式サイトから最新バージョンのリンクを取得
image.png

node-v12.18.2-linux-x64.tar.xz

ec2-linux
$ cd ~
$ wget https://nodejs.org/dist/v12.18.2/node-v12.18.2-linux-x64.tar.xz

tarファイルがあることを確認

ec2-linux
$ ls$ node-v12.18.2-linux-x64.tar

拡張子がxzになっているのでtarに変換

ec2-linux
$ mv node-v12.18.2-linux-x64.tar.xz node-v12.18.2-linux-x64.tar

解凍

ec2-linux
$ tar xvf node-v12.18.2-linux-x64.tar

色々出力されて止まったら解凍完了
image.png

実行コマンドがインストールされるnode-v12.18.2-linux-x64/binにパスを設定して移動

ec2-linux
$ export PATH=$PATH:~/node-v12.18.2-linux-x64/bin
$ cd node-v12.18.2-linux-x64/bin

app.jsというサンプルプログラムを用意

app.js
consthttp=require('http');consthostname='localhost';constport=8080;constserver=http.createServer((req,res)=>{res.statusCode=200;res.setHeader('Content-Type','text/plain');res.end('Hello World');});server.listen(port,hostname,()=>{console.log(`Server running at http://${hostname}:${port}/`);});

実行

ec2-linux
$ touch app.js
$ vim app.js

~~~~~~サンプルプログラムを貼付け(vimコマンドは各自お調べください)~~~~~~

$ node app.js
Server running at http://localhost:8080/

Viewing all articles
Browse latest Browse all 8934

Trending Articles