title: Restricting route to static files in Express and Nodejs
body: <p>I am currently trying to restrict the routes to users who haven't been logged. My main issue is that even if I define a page with a get method such as:</p>
  
  <pre><code> app.get('/alpha/information', isLoggedIn,
          function(req, res){
              res.sendFile(path.join(__dirname + '/alpha/pages/Example.html'));
          });
  </code></pre>
  
  <p>The user can sill just edit the url to: <code>http://localhost:3000/alpha/pages/Example.html</code> and access the page. Now I have read several similar questions on SO but I cannot find the answer. Some of which I was inspired were: <a href="https://stackoverflow.com/questions/11910956/how-to-protect-a-public-dynamic-folder-in-nodejs">Q1</a>,<a href="https://stackoverflow.com/questions/25732813/how-to-restrict-access-to-many-folders-in-node-js-using-express-module">Q2</a>, <a href="https://stackoverflow.com/questions/28362909/how-do-i-restrict-the-user-from-accessing-the-static-html-files-in-a-expressjs-n">Q3</a>. Nonetheless I was unable to find a solution to my issue. </p>
  
  <p>My current file structure is:
  <a href="https://i.stack.imgur.com/j3lmb.jpg" rel="nofollow noreferrer">FileStructureLink</a></p>
  
  <p>I am trying to restrict access to <strong>Example.html, ExampleTwo.html</strong> and <strong>blabla.html</strong></p>
  
  <p>I am using this code to set up the requests but I guess they might not be right:</p>
  
  <pre><code>app.use(express.static(path.join(__dirname, 'Alpha')));
  app.use(express.static(path.join(__dirname, '/')));
  app.use('/', express.static(__dirname + '/login.html'));
  </code></pre>
  
  <p>This  <code>app.use('/', express.static(__dirname + '/login.html'));</code> specifically is used to make the default <code>localhost:3000/</code> load as <code>localhost:3000/login</code></p>
  
  <p>How can I restrict access to all the static html files without having to write a route for each of them?</p>
  
  <p>middleware function:</p>
  
  <pre><code>function isLoggedIn(req, res, next) {
          console.log('here is Authenticated', req.isAuthenticated())
          if (req.isAuthenticated()){
              return next();
          }
          res.redirect('/login');
      }
  </code></pre>