Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js express vhost动态子域_Node.js_Express_Express Vhost - Fatal编程技术网

Node.js express vhost动态子域

Node.js express vhost动态子域,node.js,express,express-vhost,Node.js,Express,Express Vhost,我正在尝试使用身份验证检查创建动态子域。子域按预期工作,但由于我添加了身份验证,它只是被忽略了,至少在大多数情况下是这样。 isAuthenticated总是返回false,因此理论上它应该总是重定向到login。但事实并非如此。 此外,日志打印(在我看来)非常随机 有人能帮我吗 subdomains.forEach(subdomain => { const subDir = `${subdomainDir}/${subdomain}/`; let subExpress

我正在尝试使用身份验证检查创建动态子域。子域按预期工作,但由于我添加了身份验证,它只是被忽略了,至少在大多数情况下是这样。 isAuthenticated总是返回false,因此理论上它应该总是重定向到login。但事实并非如此。 此外,日志打印(在我看来)非常随机

有人能帮我吗

subdomains.forEach(subdomain => {
    const subDir = `${subdomainDir}/${subdomain}/`;

    let subExpress = express();

    subExpress.use(express.static(__dirname + subDir));

    subExpress.get('*', async (req, res) => {
        let isAuthenticated = false;

        try {
            isAuthenticated = await login.isAuthenticated(req.get('authorization'))
        } catch (error) {
            console.log(error);
        }

        console.log("subdomain", subdomain)
        console.log("isAuthenticated", isAuthenticated, "subdomain === login", subdomain === "login");

        if (isAuthenticated || subdomain === "login") {
            res.sendFile(`/${subDir}index.html`, {
                root: '.'
            });
        } else {
            res.redirect(301, `https://login.${domain}/`);
        }
    });

    app.use(vhost(`${subdomain}.${domain}`, subExpress));
});

let unusedSub = express();

unusedSub.get('*', (req, res) => {
    console.log("unusedSub");
    res.redirect(`https://dashboard.${domain}/`);
});

app.use(vhost(`*.${domain}`, unusedSub));

app.listen(port, () => console.log(`Listening on port ${port}.`));

我已经弄明白了。基本上,“使用”的顺序是错误的。 静态部分必须在身份验证检查之后,您必须调用“next”。 也许这会对某人有所帮助

subdomains.forEach(subdomain => {
    const subDir = `${subdomainDir}/${subdomain}/`;

    let subExpress = express();

    subExpress.use(async (req, res, next) => {
        let isAuthenticated = false;

        try {
            isAuthenticated = await login.isAuthenticated(req.get('authorization'))
        } catch (error) {
            console.log(error);
        }

        if (isAuthenticated || subdomain === "login") {
            next();
        } else {
            res.redirect(301, `https://login.${domain}/`);
        }
    });

    subExpress.use(express.static(__dirname + subDir));

    app.use(vhost(`${subdomain}.${domain}`, subExpress));
});