2

我有一个 node 应用程序作为 web 应用程序框架运行 express,我使用 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

帮助?

4

2 回答 2

3

只做:

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

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

于 2015-03-02T17:40:52.260 回答
0

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

app.use(stormpath.loginRequired, express.static(__dirname + '/public'));
于 2016-05-21T10:26:29.253 回答