Monday, 8 May 2017

How to turn off app.use(express.static('/public')); Express js

I am using Express js. In the root folder, I have a public folder which contains subfolders stylesheets, javascripts, fonts, images and html. I want to protect my assets if the user is not logged in. For example, if someone opens the web browser and without login make a request for http://localhost:3000/images/hello.jpg then the request should be redirected to login page.

To achieve this I am using express.static(path.join(__dirname, '/public')) conditionally.

Challenge 1

Login page also having some 4 to 5 assets (CSS and JS files), which are placed in the same public folder.

Challenge 2

I am using this code express.static(path.join(__dirname, '/public')) like this:

if(userIsLoggedIn){
   express.static(path.join(__dirname, '/public'))
}else{
   //what to do here???
   //I need a code here which makes public folder as non-static folder
}

For example, we have 2 users A and B. Both users are using different computers. User A is doing successful login and at the server, we are setting the public folder as static but user B has not done login he is now directly accessing the assets and he is able to access.

This is happening because we are not setting the public folder as static on user basis we are making it static if any of user do a successful login.



via Arpit Meena

No comments:

Post a Comment