1

In Node.js I made simple router, so server opens everything what user typed in URL. So user type: http://example.com/directory1/test.html and it will be opened.

But I don't want to allow to open some of directories. I want to allow user to open files which are only in directory called for example "userFiles".

How can I do that?

Piotrek
  • 10,919
  • 18
  • 73
  • 136

1 Answers1

0

Assume that we are using express.js, I offten do this in my code:

var app = express();

app.use('/whitelist', express.static('/path/to/whitelistfolder'));

Or write a filter middleware:

app.use(function (req, res, next) {
    if(checkFunction(req.url)) {
        next();
    } else {
        res.send(404, "Not found");
    }
})
damphat
  • 18,246
  • 8
  • 45
  • 59
  • 1
    Who said anything about express? Doing this without express isn't particularly hard. Please consider adding an example that does not require express. Also, this approach (blacklisting and not whitelisting) is problematic. – Benjamin Gruenbaum Dec 29 '13 at 16:30
  • I still wait for another answers :) – damphat Dec 29 '13 at 16:51
  • If no one posts a decent one in the next day or two I will. I posted the way express (connect really) does this in a comment on the answer if you want a reference and attempt this yourself. – Benjamin Gruenbaum Dec 29 '13 at 16:52
  • If It was possible (and easy to introduce), I would want to use it without express. @BenjaminGruenbaum I'm waiting for your answer ;) – Piotrek Dec 30 '13 at 13:57
  • Very well give me a day and a half :) – Benjamin Gruenbaum Dec 30 '13 at 15:22