NodeJS 和 Express:将 auth 中间件添加到静态路径

NodeJS and Express: Add auth middleware to static path

我有一个节点应用程序 运行 表示为 Web 应用程序框架,我使用 Stormpath 进行身份验证。

Storm path 提供了使用多个中间件保护路由的能力, 例如:

router.get('/user_data_api', stormpath.apiAuthenticationRequired, function(req, res) {
        res.send('hello you are authenticated!");
     });
});

我想做的是将authenticationRequired作为中间件添加到express的静态定义中:

app.use(express.static(__dirname + '/public'));

这可以通过向静态资产添加路由来实现,所以如果我有一个文件 ./public/index.html 我可以这样设置路由:

app.use('/secured-assets', 
               stormpath.auth_fn, express.static(__dirname + '/public'));

但是文件将在

www.mydomain.com/secured-assets/index.html

我想要

www.mydomain.com/index.html

帮忙?

只做:

app.use(stormpath.auth_fn, express.static(__dirname + '/public'));

它会将 stormpath.auth_fnexpress.static(__dirname + '/public') 中间件添加到 / 路径,因此会保护每条路由。

这对我适用于 Express ^4.13.4 和 Stormpath ^3.1.2

app.use(stormpath.loginRequired, express.static(__dirname + '/public'));