当前位置: 首页>NodeJS>ExpressJS>Catching all invalid urls in expressJS is also catching stat

Catching all invalid urls in expressJS is also catching stat

时间:2015-07-09 08:32 来源:网络整理 作者:KKWL 点击:
Most likely, you're calling app.use(app.router) prior to app.use(express.static(...)), so that express tries to find matching route first, and only if there is no matched route it tries to find the corresponding static files. The configurat

Most likely, you're calling app.use(app.router) prior to app.use(express.static(...)), so that express tries to find matching route first, and only if there is no matched route it tries to find the corresponding static files.

The configuration code should be like that:

app.configure(function () { app.use(express.static(__dirname + '/public')); app.use(app.router); });

so that express will try to find a static file first.

answered Aug 28 '12 at 11:48

penartur

    

Thanks it saves my time.. But this synchronous thingy has to change... expressjs have to differentiate static calls and invalid url like in other framework ROR, etc – 

    

What "synchronous thingy"? What do you call "an invalid url"? With express configured as in provided code example, express first handles the url to the static middleware; the middleware either serves the static file (if it exists) or handles the url to the next processor in line (if file does not exists), which is the router middleware; the middleware either calls your controller (if there exists a matched route) or handles the url to the next processor in line (won't happen, as you have a route /* that matches all urls). –  penartur Aug 29 '12 at 6:26

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------