0

In Express, I'm having trouble finding a way to prompt the user to enter a username/password when they hit a certain directory much like I could on an Apache server with htaccess and htpasswd. Below is some example code of where I'm trying to go with this.

app.get('/password-directory', function(req, res, next) {
   if (user === 'username' && password === "setPassword') {
     res.send(201);
   } else res.send(403);
})

Is there any way to prompt the user when they hit mysite.com/password-directory for their credentials? Or even doing this username:password@mysite.com/password-directory?

I'm really only looking to protect a specific directory, it has some static files in it so I'm really looking for the most basic solution possible.

Thanks for any help and please let me know if I need to clarify anything!

  • @Mike'Pomax'Kamermans It looks like that's on the right track but when I use that method it still displays my static content then prompts me for a user/pass. When I hit cancel I can just view my static content anyways. That's not really useful. Any thoughts on why that could be happening? – user3669257 Apr 27 '15 at 22:13
  • 1
    don't use a `static` binding for your files, use a true GET handler (or, two: one for `/yourdir` and one for `/yourdir/:filename`), with the authentication middleware in place so that it has to pass before any content is shown. For the dir readout, you can then use a lib that shows a dir listing. – Mike 'Pomax' Kamermans Apr 27 '15 at 23:11
  • @Mike'Pomax'Kamermans Thanks that solved my problem! I will be closing this thread as you've pointed out it's very similar to another and the problem I had may be specific to me. – user3669257 Apr 29 '15 at 13:11

0 Answers0