简体   繁体   English

Express-在同一路径上服务不同的静态文件夹

[英]Express - Serve different static folders on same route

Hello I would like to serve two different front-end applications on same route. 您好,我想在同一条路线上服务两个不同的前端应用程序。

User will send a token in HTTP header and based on the information that token holds application should decide which folder should it serve statically. 用户将在HTTP标头中发送一个令牌,并且基于令牌持有的信息,应用程序应确定其应静态服务于哪个文件夹。 Is that possible? 那可能吗?

I already have a middleware that parses token and provides me with user role. 我已经有一个中间件来解析令牌并为我提供用户角色。

Preferred behavior would be 首选行为是

module.exports = function (app) {
    app.get('/admin' function(req, res) {
        if(req.headers.security.role === 1) {
           // serve superadmin page
        } else if(req.headers.security.role === 2) {
           // serve user page 
        } else {
            // serve forbidden page
        }
    });

};

Of course it is, here is an example with a cookie sent with the request with nodejs. 当然可以,这是一个示例,其中cookie是通过nodejs与请求一起发送的。

app.get('*', function (request, response, next) {
   let app = request.cookies.app;
   if (app) {
     response.sendFile(path.resolve(__dirname, 'app.html')) 
   } else {
     response.sendFile(path.resolve(__dirname, 'app2.html')) 
   }
});

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

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