A basic installation of Node.js, Express, and Socket.io served over https://
using a Letsencrypt SSL certificate.
- Spin up DO droplet
- Set up DNS
- Access the server via SSH
- Update server:
apt-get update
- Install Nodejs:
apt-get install nodejs
- Install NPM:
apt-get install npm
- Install Express:
npm install express
- Build app:
npm init
- Give the app a name: Socket
- Complete other fields as desired or leave blank
- Confirm details:
yes
- Install socket.io:
npm install socket.io --save
- Visit socket.io Javascript client library
- Enter the dist folder
- Select the socket.io.js file
- Copy the files URL: https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.js
- Clone the file to the server using:
wget https://github.com/socketio/socket.io-client/blob/master/dist/socket.io.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)
- 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>
- Run node:
nodejs index.js
The site can now be viewed over http://
.
Install Certbot:
sudo apt install certbot
Now run CertBot and follow instructions, selecting 1 as the option to continue:
sudo certbot certonly
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://
.