factor_za factor_za - 1 year ago 73
Node.js Question

Changing a file directory when using socket.io and node.js?

I have created a successful connection and handled events of someone connecting and disconnecting. However I am now trying to clean up my folders to create something that is a bit more clean to work with.

I have looked at some examples of changing directories, however it seems that I am missing something.

I am quite new to using node and socket, so might seem a bit obvious with the problem.

My basic file structure is i have a main app folder, with an index.html file, server folder with all the relevant node and socket code, then a public folder with css, js, image folders.

File Structure

So my index is in the route.
I do also find when I try linking stylesheets and js files it returns a 404 error. Not entirely sure. Any help would be greatly appreciated.

// Sets a base path for acessing all files

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


Updated file structure:
enter image description here

Answer Source

You won't need the app.get, express.static handles indexes for you. You're also serving from / instead of ../public (I'm guessing based on your structure). Try this:


If it were me, I'd move the index.html into the public folder, and move your server logic into the root. That way you can do

app.use(express.static(path.join(__dirname, 'public')));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download