I'm learning node js , so i decided to create a game and was setting up my project but ran into this issue.
My Script tag pointing to external source is not working, But when i copy paste the same javascript code into the html script tag it is working, Here are My code
index.html
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Sanke Game</title>
</head>
<body>
<script src="/socket.io/socket.io.js"></script>
<script src="client.js"></script>
</body>
</html>
client.js
const sockt = io("http://localhost:3000/");
sockt.on("serverToClient", function(event){
alert(event);
})
app.js
const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app);
const { Server } = require("socket.io");
const io = new Server(server);
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html")
})
io.on('connection', function(socket){
console.log("User is connected");
socket.on("disconnect", function(){
console.log("User Disconnected");
})
socket.emit("serverToClient", "Hello World");
})
server.listen(3000, function(){
console.log("Listening to port 3000");
})
All files are in same level.