Node JS Connect file server not serving as expected
Asked Answered
T

1

0

I'm playing with a simple Connect file server:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static('.'))
    .listen(3000);

The file index.html loads when I visit localhost:3000. But I can't seem to access any other file in the way that I would expect. For example, the address localhost:3000/json-parser.html returns Error: Forbidden followed by information about the Connect module (I won't include it all here unless requested, because it's quite long and I suspect there is a simple answer to this).

I have changed my server, following code here, to serve a 'public' folder within my directory:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static('public'))
    .listen(3000);

But I want access to scripts and files within folders within the parent directory which isn't possible without putting everything in 'public' and having my Connect file server outside that. Is there a way for Connect to serve the directory around it, given that the above doesn't seem to work?

Toffee answered 9/3, 2013 at 10:11 Comment(0)
O
1

Try:

var connect = require('connect'),
    http = require('http');

connect()
    .use(connect.static(__dirname))
    .listen(3000);

However bear in mind that this will serve ALL the files and subdirectories underneath the directory where you ran server.js which is generally NOT a good plan.

Omega answered 9/3, 2013 at 10:43 Comment(3)
+1 Thank you. This does seem to allow me to serve the pages I need (so localhost:3000/json-parser.html now works) but any attempt to access other files from those pages does not seem to work in the same way. So I may need to update my question...Toffee
Actually, I think the 'update' I was going to make to my question is probably a separate question altogether. Could you elaborate on why serving the files and subdirectories underneath the directory where I run the server is not a good idea? I have a feeling it isn't too but it would be good if you could articulate it as part of your answer. Thanks.Toffee
Simply put - it allows anyone that knows a name of a file in that directory free access to it - in our case its the whole tree of the "project".Omega

© 2022 - 2024 — McMap. All rights reserved.