Stop website visitors from accessing files via URL

Hey, I have JavaScript files and I don’t really want people accessing them by doing www.glitchproject/server.js

Is there a way to stop this?

This may help.

I’m pretty sure that .htaccess files don’t work on hello-website projects. Thanks though.

Ah, it doesn’t? Sorry, someone else may be able to help you more.

I’m quite serious but are you sure that is an issue? I don’t think my websites permit the request of arbitrary js files.

Would you mind sharing your public URL so I could see it? If it serves it up at the moment we are going to solve it anyway. Could you have a route that accesses files in a folder or something?

My website is https://www.discovertechrblx.com The file name is server.js and if I do https://www.discovertechrblx.com/server.js it opens the file and displays it on the webpage which I want to stop from happening.

Oh I see it is a simple static website right? So there is no web server behind it in which case I see that happening. Why is named server.js BTW? I assume it is loaded into the html page right?

Yes, its a hello-website project that is boosted. The file is name server.js because that is what I decided to put it as. That file handles requests etc and sends pages files depending on the request URL.

Ok I’m confused :slight_smile: There is a server. That is very cool 503 page BTW.

Okay, and thank you my friend made most of it though!

Agreed, looks sick.

I’m typing quicker than I normally would but it appears that you have at least an option set to serve static pages from the folder.

Application.use(Express.static(__dirname));

I don’t use Express and I would ordinarily confirm my suspicions but is this what that does?

Also you added Express to that right? the hello-website is static i.e. there is no server.

Yes, it serves all files on the website.

So it is doing what you said to do. Basically you “hide” the files in a folder nobody knows about if you don’t want them served or better yet (I think) you put all the files you do want served in a special public folder.

Ah no I didn’t set it in a public folder thats where I went wrong. I will change the Application.use(Express.static(__dirname)); to Application.use(Express.static(__dirname + “/public”));

That should stop the files from being accessed right?

Yup. You told it to server them from the root folder. You don’t need to concatenate the root folder however, that is implied.

As per the instructions it even supports multiple folders.

app.use(express.static('public'))
app.use(express.static('files'))

Awesome, thank you it did work!