Node.js Express 4 intercept failed static route -
i'm building web site requiring secure zone protected username/password (form authentication).
here structure :
- project folder |- lib |- file , sub folders |- public |- fonts |- file , sub folders |- images |- file , sub folders |- javascript |- file , sub folders |- stylesheets |- file , sub folders |- file @ root of public folder |- routes |- login.js |- home.js |- others files... |- views |- layout |- file , sub folders |- file @ root of views folder
now don't want protect file in public folder, of thoses static file, can serve directly.
i want protect secure zone, routes views except 1 going login controller.
here i've done far :
// ... var express = require('express'); var expressapp = express(); expressapp.set('port', httpport); expressapp.set('views', path.join(__dirname, 'views')); expressapp.set('view engine', 'ejs'); expressapp.use(favicon(__dirname + '/public/images/favicon.ico')); expressapp.use(express.static(path.join(__dirname, 'public'))); // if static file has not been found, // issue 404 response , stop routing here expressapp.get(['/public', '/public/*'], function (req, res) { res.send(404, 'resource not found !'); }); // configure middleware expose request object in scoped ejs view expressapp.use(function (req, res, next) { res.locals.request = req; next(); }); // test if user authenticated before routing // page other login var loginpage = require("./routes/login"); expressapp.use(["/login", "/login/*"], loginpage); var ensureuserauthenticated = function (req, res, next) { if (!req.session.user) { // redirect login page } else { // evaluate next route next(); } }; // configure remaining routes expressapp.use("/", ensureuserauthenticated, require("./routes/index")); expressapp.use("/home", ensureuserauthenticated, require("./routes/home")); expressapp.use("/settings-fan-settings", ensureuserauthenticated, require("./routes/settings-fan-settings")); expressapp.use("/settings-fan-settings-edit", ensureuserauthenticated, require("./routes/settings-fan-settings-edit")); // route resource not found expressapp.use(function (req, res, next) { res.status(404).send('404 - page not found !'); });
the problem if i'm pointing resource (static) not exists (ex : "/javascripts/foo.js", middleware trying find file until last 1 returning message "page not found".
i've tried next solution same problem. "page not found" middleware respond 404, not 1 replying "resource not found".
expressapp.use(express.static(path.join(__dirname, 'public'))); var staticfileresourcenotfound = function (req, res) { res.send(404, 'resource not found !'); }; expressapp.get('/public', staticfileresourcenotfound); expressapp.get('/public*', staticfileresourcenotfound); ... expressapp.use("/", ensureuserauthenticated, require("./routes/index")); ... expressapp.use(function (req, res, next) { res.status(404).send('404 - page not found !'); });
why middleware having message "resource not found" not triggered ? put breakpoint never reach.
i've tried many thing i'm out of idea now.
best regards
the request "/javascript/foo.js" not match route
expressapp.get(['/public', '/public/*'], ...
you need define path javascript files like
"/public/javascript/foo.js"
and change route remove array of paths (no longer supported app.verb in express 4.0)
expressapp.get('/public', handlerequestfunction()); expressapp.get('/public*', handlerequestfunction());
Comments
Post a Comment