Javascript 如何在我的所有网页中显示用户名。。。?

Javascript 如何在我的所有网页中显示用户名。。。?,javascript,node.js,Javascript,Node.js,我尝试在成功登录后在我的所有网页上显示用户名,在这里我可以在我使用的ejs的主页上捕获用户名,但是如果客户端想从主页移动到其他页面,用户名不可见,在这里我错过了一些如何在所有页面上处理此用户名的逻辑 提前谢谢 app.post('/Home', function(request,res){ console.log(request.body); const check = "select count(*) as cnt from users_info where Nam

我尝试在成功登录后在我的所有网页上显示用户名,在这里我可以在我使用的ejs的主页上捕获用户名,但是如果客户端想从主页移动到其他页面,用户名不可见,在这里我错过了一些如何在所有页面上处理此用户名的逻辑

提前谢谢

app.post('/Home', function(request,res){     
    console.log(request.body);
     const check = "select count(*) as cnt from users_info where Name ='"+request.body.userid +"' && Password = '"+request.body.password +"' ";
    mysqlConnection.query(check,function(err,result){
        console.log(result);
        var Records= result[0].cnt;
        console.log(Records);
        if (Records== 1)
        {
            response.render('Login', {username:request.body.userid});
        }
        else
           console.log('User name or Password is invalid');
            //  res.render('Login.ejs', {error: 'User name or Password is invalid'});
    });  
    }); 

app.post("/Login/search", (request,response) =>{
    console.log(request.body.search) 
    const check = "select * from users_info where Name NOT LIKE '${request.body.search}%' ";



 mysqlConnection.query(check,function(err,result){
        const arr = result ;
        console.log(app)
        console.log(arr.length)
        let countRecords = arr.length;
        if (countRecords>0){
            const names = arr.map(({ Name }) => Name).join('\n');
            console.log(names.length)
            console.log(names)

            res.render('Login-success', {users:names});

            // res.send(names);
        } else {
            console.log('Request failed...')
        }


    });  
});
header.ejs

<body>
         welcome  <%=  username%>

欢迎

如何在Home.ejs、search.ejs等页面上使用此头文件…

您可以创建名为“header.ejs”的文件

欢迎您
对于其余网页,可以使用include命令包含header.ejs文件

<% include header.ejs %>


注:我认为,HealE.EJS和所有其他文件都位于同一个文件夹中。

< P>可以创建名为“Health.EJS”的文件。

欢迎您
对于其余网页,可以使用include命令包含header.ejs文件

<% include header.ejs %>


注释:我认为,HealE.EJS和所有其他文件都位于同一个文件夹中。

< P>可以用渲染方法< /P>传递AuthObjt对象。
res.render('Login-success', { auth: req.session.auth });
然后

welcome <%= auth.name %>
欢迎您

可以使用渲染方法传递auth对象

res.render('Login-success', { auth: req.session.auth });
然后

welcome <%= auth.name %>
欢迎您

感谢您的回复,这里我得到的错误是在移动到/Login/search from/Home page后没有定义用户名,我的意思是在app.post(/Home){----------}中呈现文件时必须提到用户名:res.body.userid吗如果是强制性的,那么在呈现文件res.render('Login-success',{users:names})时如何提及这个用户名:res.body.userid;在app.post(/Login/search){----------}///这里我如何实现您可以在登录后获得用户名后设置cookie值。完成setcookie后,在header.ejs页面下,您可以像使用jquery和javascript一样编写,获取用户名的值并设置为span标记。一旦你对header.ejs文件这样做了,你就可以在每个页面上都有用户名了。感谢你的回复,这里我得到的错误是在移动到/Login/search from/Home page后用户名没有定义,我的意思是在app.post(/Home){-----中呈现文件时必须提到用户名:res.body.userid吗如果是强制性的,那么在呈现文件res.render('Login-success',{users:names})时如何提及这个用户名:res.body.userid;在app.post(/Login/search){----------}///这里我如何实现您可以在登录后获得用户名后设置cookie值。完成setcookie后,在header.ejs页面下,您可以像使用jquery和javascript一样编写,获取用户名的值并设置为span标记。对header.ejs文件执行此操作后,您就可以在每个页面上使用用户名了。