dimanche 6 novembre 2016

Route an entire folder and its content

I want to protect a folder and its content by redirecting the user back to index.

I've tried this, but it only works partially.

var express = require('express');
var path = require('path');
var http = require('http');
var app = express();


app.set('port', 8080);
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'views')));

app.get('/', function(req, res) {
   res.render('index.ejs'); 
});

app.get('/protected/*', function(req, res, next) {
    res.redirect('/');
    next(); 
});

//activating server
http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

This routes, for example, "localhost:8080/protected" and "localhost:8080/protected/asdf", but not "localhost:8080/protected/otherPage.html".

In this case asdf is not an actual file, but otherPage.html is. So if the file is there it doesn't redirect, but if it is not then it redirects. Why is this?




Aucun commentaire:

Enregistrer un commentaire