Why isn't my code getting executed successfully

server.js :-

const http = require(‘http’)

const express = require(‘express’)
const app = express()

const socketio = require(‘socket.io’)
const server = http.createServer(app)

const io = socketio(server)

io.on(‘connection’ , (socket)=>{
//this function takes a socket argument and connects it to the socket id

console.log("Connected with socket id= ", socket.id)

socket.on('boom', ()=>{
    //this function will run if the boom event happens 
    console.log('boom received from ', socket.id)
})

setInterval(() =>{
    //emit whizzz after every 2sec
    socket.emit('whizzz')
},2000)

})

app.use(’/’, express.static(__dirname + ‘public’))

server.listen(3344,()=>{
console.log(‘Started on http://localhost:3344’)
})

public>index.html:-

Document
    <script src="/socket.io/socket.io.js"></script>
    <script defer src="script.js"></script>

</head>

<body>
    <h1>Socket.IO Sample</h1>
    <button id="boom">Boom</button>
</body>

public > script.js

let socket = io()

let boombtn = document.getElementById(‘boom’)
boombtn.onclick = function() {
socket.emit(‘boom’)
//socket.emit -> so as to emit events into a socket

socket.on('boom', () => {
    console.log('boom received from ', socket.id)
})

}

ERROR:-

/usr/local/bin/node Client_to_sever/public/script.js
/Users/deeptisharma/Desktop/socketIO /Client_to_sever/public/script.js:1
let socket = io()
^

ReferenceError: io is not defined
at Object. (/Users/deeptisharma/Desktop/socketIO /Client_to_sever/public/script.js:1:14)
at Module._compile (internal/modules/cjs/loader.js:1138:30)
at Object.Module._extensions…js (internal/modules/cjs/loader.js:1158:10)
at Module.load (internal/modules/cjs/loader.js:986:32)
at Function.Module._load (internal/modules/cjs/loader.js:879:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
at internal/main/run_main_module.js:17:47

check if the file

/socket.io/socket.io.js

is hosted correctly, check the network tab to see if the fetch request results in 200 or not
try adding the exact path instead of relative path like this

<script src="http://localhost:PORT/socket.io/socket.io.js"></script>

see if this work

it’s still not working

@sharma.deepti182 have you checked if socket.io.js file has been hosted properly, just run this

http://localhost:PORT/socket.io/socket.io.js

after starting the server and see if it returns anything.
Where have you hosted your public files, they should be present at base route i.e. ‘/’

I did the same but the web page says cannot ‘get’
And I think something’s wrong with the script.js file, I changed the script src thing but it the condition remains same. Please help me out with this.

@sharma.deepti182 share your code here, after uploading it on github. So that, I can have a better view at what’s is going wrong.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.