繁体   English   中英

通过带有重定向和中间件的Express服务出“静态”路由

[英]Serve out 'static' route with express with redirect and middleware

我正在用Express 4.0提供静态网址:

app.use('/ static-route',express.static('./ static'));

而且效果很好。

但是,如果用户点击该路由,我想将其重定向到带有查询参数的网址。

即/ static-route-> / static-route?someQueryParam = hello

我还想为该静态请求包括中间件。 作为一个具体示例,我正在使用护照,并希望确保用户已登录以访问该静态内容。

app.use(和app.get等..)没有两个参数,第一个参数是route(可选),然后其余都是中间件。

app.use('/static-route', function (req, res, next) {
  // validation
  // redirect
  // etc . . .
  next();
}, express.static('./static'));
  • 使用全局通配符路由[app.use('/')]获取静态内容,并
  • 使用特定的路由[app.get(/ myroute),app.post('/ anotherroute')]使用自定义逻辑进行动态处理
//Serves resources from public folder 
app.use('/',express.static(__dirname + '/public')); 

//Verify the complete directory path - especially slashes 
console.log('Static directory '+__dirname + '/public');

app.get('/list', function (req, res) {
    res.send('<html><body><h1>Hello World</h1></body></html>'); });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM