简体   繁体   English

Express-公用目录划分为授权/未经授权的用户

[英]Express - public directory divided for authorized/unauthorized users

I have an app written in express.js and I'm trying to divide this application to 2 sections: 我有一个用express.js编写的应用程序,并且试图将此应用程序分为2个部分:

  • one for unauthorized users (with routes only to / - landing page, /login and /* - error404) 一个用于未经授权的用户(仅具有到/-登陆页面,/ login和/ *-error404的路由)
  • and second (routes will be: / - landing page, /app/* - angular SPA which will handle routing on its own) 第二个(路线为:--登陆页面,/ app / *-角度SPA,它将自行处理路线)

Express is also configured to take static files from /unauth/public/ And I want to add second static folder for request from authorized routes - /auth/public which goes to /app/* Express还配置为从/unauth/public/获取静态文件,并且我想添加第二个静态文件夹以请求来自授权路由的请求- /auth/public转到/ app / *

My route config looks like this: 我的路线配置如下所示:

var authRoutes = express.Router();
var unauthRoutes = express.Router();

authRoutes.get('/app/*', function(req, res, next) {
    if(!req.isAuthenticated())
        return res.redirect("/login/");
    res.send("AUTHORIZED");
});

unauthRoutes.get('/', function(req, res, next) {
    res.send("LANDING PAGE");
});

unauthRoutes.get('/login/', function(req, res, next) {
    if(req.isAuthenticated())
        return res.redirect("/app/");
    res.send("LOGIN PAGE");
});

unauthRoutes.get('/registration/', function(req, res, next) {
    if(req.isAuthenticated())
        return res.redirect("/app/");
    res.send("REGISTRATION PAGE");
});

unauthRoutes.get('/*', function(req, res, next) {
    res.send("ERROR 404");
});

app.use('/', authRoutes);
app.use('/', unauthRoutes);

I tried to modify req.url and call another static oruter express.static('auth/public') based on this: 我试图修改req.url并基于此调用另一个静态oruter express.static('auth/public')

Using express.static middleware in an authorized route 在授权路由中使用express.static中间件

But I don't know, how to handle route app.get('/auth/*', ...) - previous modification will replace url and this route will never be called.. 但是我不知道如何处理路由app.get('/auth/*', ...) -之前的修改将替换url,并且永远不会调用此路由。

You could try something like this: 您可以尝试这样的事情:

// Create your static middlewares
var unauthStatic = express.static('unauth/public');
var authStatic = express.static('auth/public');

// This goes in place of where you would normally load your static middleware
app.use(function(req, res, next) {
    if (req.isAuthenticated()) {
        authStatic(req, res, next);
    } else {
        unauthStatic(req, res, next);
    }
});

edit: 编辑:

if you want authenticated users to be able to access files from both the auth and unauth directories, you can make two calls to app.use , like this: 如果希望通过身份验证的用户能够访问auth和unauth目录中的文件,则可以对app.use进行两次调用,如下所示:

app.use(unauthStatic);
app.use(function(req, res, next) {
    if (! req.isAuthenticated()) {
        return next();
    }
    authStatic(req, res, next);
});

Remember that express uses middleware in a stack, meaning to serve a given request, all registered middleware is used in the order it's use d. 请记住,express在堆栈中使用中间件,这意味着要满足给定的请求,所有已注册的中间件均按其use d的顺序use Once a bit of middleware calls req.send, no further middleware gets executed. 一旦一些中间件调用req.send,就不会再执行其他中间件。 Anyway, try something like this: 无论如何,尝试这样的事情:

function Authorization(req, res, next) {
    if(!req.isAuthenticated())
        return res.redirect("/login");
    next();
}

var AnonRouter = express.Router()
    // GET /style.css will request /unauth/public/style.css
    .use(express.static('unauth/public'))
    .get('/', function (req, res) { })
    .get('/login', function (req, res) { });

var AuthRouter = express.Router()
    .use(Authorization)
    // GET /app/style.css will request /auth/public/style.css
    .use(express.static('auth/public')) 
    .get('*', function (req, res, next) {
       // Handle reqs for non-static files
    });

app.use('/', AnonRouter);
app.use('/app', AuthRouter);
app.get('*', function (req, res) {
    res.status(404).send('404!');
});

But I don't know, how to handle route app.get('/auth/*', ...) - previous modification will replace url and this route will never be called.. 但是我不知道如何处理路由app.get('/ auth / *',...)-之前的修改将替换url,并且永远不会调用此路由。

This statement makes me think that you are trying to somehow handle the request after express's static middleware has been called. 该语句使我认为,在调用express的静态中间件 ,您正在尝试以某种方式处理请求。 This is not possible: the static middleware serves static files and you cannot execute additional logic after it does so, but you can run stuff before! 这是不可能的:静态中间件提供静态文件,并且这样做后您将无法执行其他逻辑,但是您可以在之前运行东西! Note that in my code, the Authorization middleware will run before the static file is sent. 请注意,在我的代码中,授权中间件将在发送静态文件之前运行。

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

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