Invalid or unexpected token when loading into the client side

Viewed 242

I'm trying to run an extremely simple socket.io/express webserver just to test things out. I'm using express to serve the following index.html

<html>
    <head>
        <script src="/socket.io/socket.io.js"></script>
        <script>
            const socket = io();
            console.log("We have sent a request to connect"); 
        </script>
    </head>
    <body>
        <h1>Hello Websocket</h1>
    </body>
</html>

The HTML loads fine but for some reason, I'm getting an error Uncaught SyntaxError: Invalid or unexpected token for socket.io.js 1. When I look at it on chrome there are a bunch of weird characters 摯⁥‽湥潣敤਻敹獡⹴敤潣敤㴠搠捥摯㭥洊摯汵⹥硥潰瑲⁳‽敹獡㭴ਊ⨯⨪ ⥽ਊ⨯ However, when I simply request localhost:5000/socket.io/socket.io.js I get the socket.io code I would expect.

This is my index.js:

const app = require("express")();
const httpServer = require("http").createServer(app);
const options = { /* ... */ };
const io = require("socket.io")(httpServer, options);
const path = require('path');

app.get('/', (req, res) =>{
    console.log("Getting a request to /")
    res.sendFile(path.join(__dirname, 'public' , "index.html"));
})


io.on("connection", socket => {
    console.log("We are connected");
    console.log(socket.handshake.adress);
 });

httpServer.listen(5000, () => {
    console.log("Listening on port 5000");
});

How to fix this?

0 Answers
Related