NodeJS 如果用户返回到ExpressJS中的上一页,我如何销毁会话?

zfciruhq  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(104)

我试图使一个管理员登录页面,我希望系统摧毁的admin_email_session,如果用户回到上一页。
我问ChatGPT,它给了我这个解决方案,但它没有工作。我如何解决这个问题?

app.get("/Admin-Panel",function(req,res){

        console.log(req.session.admin_email_session);

        if(req.session.admin_email_session===undefined){

            console.log("asd");
        }

        else{

            fs.readFile("Admin-Panel/admin-panel.html","utf-8",function(err,data){

                if(err){
                    throw err;
                }
                else{

                    res.write(data);
                    res.end();
                }
            });

             //True and False are parameters

            app.get("/Admin-Panel-true",function(req,res){

                req.session.destroy;
            });

            app.get("/Admin-Panel-false",function(req,res){

                req.session.destroy;
            });
        }
        
    });

字符串

pengsaosao

pengsaosao1#

你可以做这样的事

`// Check if 'admin_email_session' exists in the session, if not, destroy the session
if (!req.session.admin_email_session) {
  req.session.destroy();
}

// Send the 'admin-panel.html' file to the client
const fs = require('fs');

fs.readFile('Admin-Panel/admin-panel.html', 'utf-8', (err, data) => {
  if (err) {
    throw err;
  }

  res.end(data);
});`

字符串

相关问题