AlmaLinux 9
Sponsored Link

Node.js 16 : Install2023/03/10

Install Node.js 16.
[1] Install Node.js 16 and try to run test script.
[root@dlp ~]#
dnf -y install nodejs

 Package             Arch      Version                       Repository    Size
 nodejs              x86_64    1:16.14.0-5.el9               appstream    215 k
Installing dependencies:
 nodejs-libs         x86_64    1:16.14.0-5.el9               appstream     14 M
Installing weak dependencies:
 nodejs-docs         noarch    1:16.14.0-5.el9               appstream    6.9 M
 nodejs-full-i18n    x86_64    1:16.14.0-5.el9               appstream    8.1 M
 npm                 x86_64    1:8.3.1-       appstream    2.2 M

Transaction Summary
Install  5 Packages

[root@dlp ~]#
node -v


# verify to create test script

[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");

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

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

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

[2] Create a sample chat apprication which WebSocket and SSL/TLS are implemented to verify working.
For SSL certificate on this example, it uses the one from Let's enctypt.
If Firewalld is running, allow port the apprication uses. (it's [1337] on this example)
[alma@dlp ~]$
npm install fs express
[alma@dlp ~]$
vi chat.js
var fs = require('fs');
var express = require('express');
var app = express();

var opts = {
  key: fs.readFileSync('/etc/letsencrypt/live/').toString(),
  cert: fs.readFileSync('/etc/letsencrypt/live/').toString(),

var http = require('https').Server(opts, app);
var io = require('')(http, opts);

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');

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

[alma@dlp ~]$
node chat.js

listening on *:1337
[3] Access to the sample apprication to verify it works normally.
Matched Content