CentOS Stream 8
Sponsored Link

Node.js 18 : インストール2022/08/12

 
Node.js 18 をインストールします。
[1] 現在、有効になっている Node.js のバージョンを確認してインストールします。
[root@dlp ~]#
dnf module list nodejs

CentOS Stream 8 - AppStream
Name      Stream    Profiles                                Summary
nodejs    10 [d]    common [d], development, minimal, s2i   Javascript runtime
nodejs    12        common [d], development, minimal, s2i   Javascript runtime
nodejs    14        common [d], development, minimal, s2i   Javascript runtime
nodejs    16        common [d], development, minimal, s2i   Javascript runtime
nodejs    18        common, development, minimal, s2i       Javascript runtime

Hint: [d]efault, [e]nabled, [x]disabled, [i]nstalled

# 他バージョンが有効な場合は一旦リセットして有効バージョンを切り替え

[root@dlp ~]#
dnf module reset nodejs

[root@dlp ~]#
dnf module -y enable nodejs:18
# Node.js 18 を指定してインストール

[root@dlp ~]#
dnf module -y install nodejs:18/common

Dependencies resolved.
=============================================================================================
 Package            Arch    Version                                          Repo        Size
=============================================================================================
Installing group/module packages:
 nodejs             x86_64  1:18.6.0-1.module_el8.7.0+1189+0ca971e9          appstream   13 M
 npm                x86_64  1:8.13.2-1.18.6.0.1.module_el8.7.0+1189+0ca971e9 appstream  1.9 M
Installing weak dependencies:
 nodejs-docs        noarch  1:18.6.0-1.module_el8.7.0+1189+0ca971e9          appstream  9.3 M
 nodejs-full-i18n   x86_64  1:18.6.0-1.module_el8.7.0+1189+0ca971e9          appstream  8.0 M
Installing module profiles:
 nodejs/common

Transaction Summary
=============================================================================================
Install  4 Packages
.....
.....

[root@dlp ~]#
node -v

v18.6.0

# テストスクリプトを作成して動作確認

[root@dlp ~]# cat > nodejs_test.js <<'EOF' 
var http = require('http');
var server = http.createServer(function(req, res) {
  res.write("Hello, This is the Node.js Simple Web Server!\n");
  res.end();
}).listen(8080);
EOF 

[root@dlp ~]#
node nodejs_test.js &

[1] 1954
[root@dlp ~]#
curl localhost:8080

Hello, This is the Node.js Simple Web Server!
[root@dlp ~]#
kill 1954

[2] 任意のユーザーで WebSocket を利用した簡易チャットを作成して動作確認します。
[cent@dlp ~]$
npm install fs socket.io express
[cent@dlp ~]$
vi chat.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
  });
});

http.listen(1337, function(){
  console.log('listening on *:1337');
});

[cent@dlp ~]$
vi index.html
<!DOCTYPE html>
<html>
<head>
<title>WebSocket Chat</title>
</head>
<body>
<form action="">
<input id="sendmsg" autocomplete="off" /><button>Send</button>
</form>
<ul id="messages" style="list-style-type: decimal; font-size: 16px; font-family: Arial;"></ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
<script>
  var socket = io();
  $('form').submit(function(){
    socket.emit('chat message', $('#sendmsg').val());
    $('#sendmsg').val('');
    return false;
  });
  socket.on('chat message', function(msg){
    $('#messages').append($('<li style="margin-bottom: 5px;">').text(msg));
  });
</script>
</body>
</html>

[cent@dlp ~]$
node chat.js

listening on *:1337
[3] 任意のクライアントコンピューターで Webブラウザーを起動し、アプリケーションにアクセスして動作確認します。 複数クライアント または 複数ブラウザーを起動すると、動作が確認しやすいでしょう。
関連コンテンツ