Home > Software design >  How can resolve **Uncaught Reference Error: io is not defined at client.js:1:16** error?
How can resolve **Uncaught Reference Error: io is not defined at client.js:1:16** error?

Time:02-12

I am trying to create a real time chat app project using node and socket but when I try to run this using nodemon then it gives me error like

Uncaught ReferenceError: io is not defined
    at client.js:1:16

and

GET http://localhost:3000/socket.io/socket.io.js net::ERR_ABORTED 404 (Not Found)

plz help me to get out from this frustration. here are respective code:-

const express=require("express");

const http=require("http");

const app=express();

const server=http.createServer(app);

const io=require("socket.io")(server)

app.get("/",function (req,res) {
    res.sendFile(__dirname "/index.html");
})

app.use(express.static(__dirname "/public"))

io.on("connection",(socket)=> {
    console.log('Connected');

})

app.listen(process.env.PORT || 3000,function(){
    console.log('Running....');
})

code at client.js:-

const socket = io()
let name;
let textarea=document.querySelector("#textarea");
let msgArea=document.querySelector(".mssg_area");
do{
    name = prompt("what do your friends call you?");
}while(!name);

textarea.addEventListener("keyup",function (e) {
    if(e.key==="Enter"){
        sendMessage(e.target.value);
    }
})

function sendMessage(message) {
    let msg={
        user:name,
        message:message
    }
    appendMessage(msg,"outgoing")
}

function appendMessage(msg,type) {
    let mainDiv=document.createElement("div")
    let className=type
    mainDiv.classList.add(className,"message")

    let markUp=`
    <h4>${msg.user}</h4>
    <p>${msg.message}</p>
    `
    mainDiv.innerHTML=markUp
    msgArea.appendChild(mainDiv)
}

It would be very helpful to me if you resolve this issue. Thank you in advance!!

CodePudding user response:

Be sure, you have installed socket.io module. If not installed, install this by using command - npm install socket.io

CodePudding user response:

on the client side you should install socket.io-client not socket.io

Client Api

  • Related