Cannot use basic authentication while serving static files using express

前端 未结 4 1717
谎友^
谎友^ 2021-02-12 19:29

Using the Express framework for node.js, I\'m trying to serve up static files contained in a directory while also putting basic authentication on it. When I do so, I am prompted

4条回答
  •  醉梦人生
    2021-02-12 20:03

    Im sharing how it worked out for me.

    app.use("/login", (req, res, next) => {
      if (req.cookies.UUID) {
        res.redirect("/app");
      } else {
        next();
      }
    });
    app.use("/login", express.static("dist_auth_app"));
    app.use("/app", (req, res, next) => {
      if (!req.cookies.UUID) {
        res.redirect("/login");
      } else {
        next();
      }
    });
    app.use("/app", express.static("dist"));
    

提交回复
热议问题