Skip to content

Instantly share code, notes, and snippets.

@danmaby
Created April 1, 2022 12:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save danmaby/1f967948c31e1fe123aada11b07ac171 to your computer and use it in GitHub Desktop.
Save danmaby/1f967948c31e1fe123aada11b07ac171 to your computer and use it in GitHub Desktop.
Install SSL with Node.js, Express and Socket.io

Getting started

A basic installation of Node.js, Express, and Socket.io served over https:// using a Letsencrypt SSL certificate.

Step 1 - Build DO Droplet (or server of your choice)

  • Spin up DO droplet
  • Set up DNS
  • Access the server via SSH

Step 2 - Install dependencies

  • Update server: apt-get update
  • Install Nodejs: apt-get install nodejs
  • Install NPM: apt-get install npm
  • Install Express: npm install express

Step 3 - Create node app

  • Build app: npm init
  • Give the app a name: Socket
  • Complete other fields as desired or leave blank
  • Confirm details: yes

Step 4 - Install socket.io

  • Install socket.io: npm install socket.io --save

Step 5 - Install socket.io Javascript client library

wget https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.js

Step 6 - Create index.js

  • Create a file called index.js: nano index.js
  • Add the following to the new index.js:
var app = require('express')()
var server = require('http').Server(app)
var io = require('socket.io')(server)

var count = 0

app.get('/', function(request, response) {

  response.sendFile('/root/index.html')

})

io.on('connection', function(data) {

  count++
  data.send(count + " active sockets")

})

server.listen(80)

Step 7 - Create index.html

  • Create a file called index.html: nano index.html
  • Add the following to the new index.html:
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io()

socket.on('message', function(data) {

  document.write(data)

})

</script>

Step 7 - Run the node server

  • Run node: nodejs index.js

The site can now be viewed over http://.

Install Letsencrypt SSL

Install Certbot:

sudo apt install certbot

Now run CertBot and follow instructions, selecting 1 as the option to continue:

sudo certbot certonly

Set up index.js

Update index.js to include the following, replacing EXAMPLE.COM with the live domain:

var app = require('express')()
//var server = require('http').Server(app)
var https = require('https');
var fs = require( 'fs' );
var server = https.createServer({     
  key: fs.readFileSync('/etc/letsencrypt/live/EXAMPLE.COM/privkey.pem'),     
  cert: fs.readFileSync('/etc/letsencrypt/live/EXAMPLE.COM/cert.pem'),     
  ca: fs.readFileSync('/etc/letsencrypt/live/EXAMPLE.COM/chain.pem'),     
  
  requestCert: false,     
  rejectUnauthorized: false },app);

var io = require('socket.io')(server)

var count = 0

app.get('/', function(request, response) {

  response.sendFile('/root/index.html')

})

io.on('connection', function(data) {

  count++
  data.send(count + " active sockets")

})

server.listen(443);

Run node nodejs index.js and view site over https://.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment