title: Node.js how to use socket.io in express route
body: <p>In one of my node.js script i am trying to use socket.io in express route. I found many similar questions and tried to implement the solution as suggested but nothing worked out. May be because of my lack of understanding of express routes. I followed below links,</p>
  
  <p><a href="https://stackoverflow.com/questions/50035299/how-use-socket-io-in-express-routes-with-node-js">How use socket.io in express routes with node.js</a></p>
  
  <p><a href="https://stackoverflow.com/questions/47837685/use-socket-io-in-expressjs-routes-instead-of-in-main-server-js-file?rq=1">Use socket.io in expressjs routes instead of in main server.js file</a></p>
  
  <p>This is my app.js</p>
  
  <pre><code>const express = require('express');
  const app = express();
  const server = require('http').createServer(app);
  const io = require('socket.io').listen(server);
  
  const PORT = 3000;
  server.listen(PORT);
  console.log('Server is running');
  var api = require('./routes/api');
  
  //app.use('/api', api);
  app.use('/api', (req, res) =&gt; {
  res.sendFile(__dirname + '/api.html');
  });
  
  app.get('/', (req, res) =&gt; {
     res.send("this is home location");
  });
  </code></pre>
  
  <p>And route file api.js in ./routes folder</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var fs = require("fs");
  var bodyParser = require('body-parser');
  
  const app = express();
  const server = require('http').createServer(app);
  const io = require('socket.io').listen(server);
  
  console.log("inside api route");
  
  router.get('/', function(req, res, next) {
  console.log("api route called");
  
  const connections = [];
  var jsonobj = [{name:"john",score:345},{name:"paul",score:678}]
  
  io.sockets.on('connection',(socket) =&gt; {
      connections.push(socket);
      console.log(' %s sockets is connected', connections.length); // this is not printing
  
      socket.on('disconnect', () =&gt; {
         connections.splice(connections.indexOf(socket), 1);
      });
  
      socket.emit('server message', jsonobj);
  
   }); 
      //res.send(jsonobj) 
  });
  
  module.exports = router;
  </code></pre>
  
  <p>Socket.emit is not showing data on html page i am rendering on route use. My html code is, </p>
  
  <p>//api.html</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  
  &lt;body&gt;
     &lt;div class="container"&gt;
        &lt;h1 class="jumbotron"&gt;
           Node js Socket io with  socket route example
        &lt;/h1&gt;
        &lt;div class="results"&gt;results&lt;/div&gt;      
     &lt;/div&gt;
     &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"&gt;&lt;/script&gt;
     &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"&gt;&lt;/script&gt;
     &lt;script&gt;
      jQuery(document).ready(function() {
         var socket = io.connect();
         var jsondata = "";
  
         socket.on('server message', function(data){
           console.log('got data from server',data)
           jsondata = JSON.stringify(data);
           //console.log('jsondata',jsondata)
           $('.results').html(jsondata);
         });
      });   
   &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Please suggest what i am supposed to get route socket data in html page.
  Thanks</p>