Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js Express.js secret static files

I'm building a single page admin panel for my website in node express.

I've decided to have one admin view file and to load parts of html into it via jQuery or AJAX or something similar (did not decide yet).

I have to serve these files from static files folder (because i have to load them client side), but problem here is that these files are now avaible for everyone (also .css .js files etc...).

Any possibility how to hide these files from users? Or how to hide certain files or load files on client side which are not public?

And i really dont want to insert all my .css and .js code inside my admin view file.

SOLUTION

Found a solution here : How to secure a static route with Express and Nodejs

Sorry for duplicate, i did not find it before.

like image 775
Martin Hetfield Mali Avatar asked Aug 03 '26 07:08

Martin Hetfield Mali


1 Answers

You should use some kind of authentication like passport and a middleware could be helpful to protect your static files:

app.use('/admin', function(req,res,next){
 if(req.user){
   return express.static(path.join(__dirname, 'public'));
 } else {
   res.render(403, 'login', {message:'Please, login!'});
 }
});
like image 135
Andrés Andrade Avatar answered Aug 05 '26 22:08

Andrés Andrade