Skip to content
Advertisement

JS file gets a net::ERR_ABORTED 404 (Not Found)

I am trying to create a simple Io-web-chat. I recently wanted to seperate my <script> inside my html file to an external js file.

this is my very simple folder structure:

JavaScript

Relevant part of html file:

JavaScript

Relevant part of index.js file:

JavaScript

Relevant part of server.js file:

JavaScript

I also tried putting my files in this public type structure that they have on the socket.io examples:

JavaScript

in that case I changed: src="/index.js" in html added /public/index.html into the server.js file But no luck.

This is all running in localhost. What am I doing wrong here?

Advertisement

Answer

As mentionned in comments: you need a way to send your static files to the client. This can be achieved with a reverse proxy like Nginx, or simply using express.static().

Put all your “static” (css, js, images) files in a folder dedicated to it, different from where you put your “views” (html files in your case). I’ll call it static for the example. Once it’s done, add this line in your server code:

JavaScript

This will effectively serve every file in your “static” folder via the /static route.

Querying your index.js file in the client thus becomes:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement