How to do it...

The Socket.IO server application will expect the users to be logged-in in order for them to be able to connect to the /home namespace. Using socket middleware, we will also restrict access to /home namespace to a certain user:

  1. Create a new file named middleware-server.js
  2. Include the Socket.IO library and initialize a new HTTP server:
 const http = require('http') const fs = require('fs') const path = require('path') const io = require('socket.io')() const app = http.createServer((req, res) => { if (req.url === '/') { fs.readFile( path.resolve(__dirname, 'middleware-cli.html'), (err, data) => { if (err) { res.writeHead(500) return void res.end() } res.writeHead(200) res.end(data) } ) } else { res.writeHead(403) res.end() ...

Get MERN Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.