简体   繁体   English

发现模板引擎(ejs)问题

[英]Template Engine (ejs) issues found

  SUBMIT EJS FILE <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <h2> Your password has been changed click here for <a href="user">login</a></h2> </body> </html> 

MY PASSWORD CLASS 我的密码课

class Password { 类密码{

constructor(newPass,confirmPass) {

    this.newPass=newPass;
    this.confirmPass=confirmPass;    
}

} module.exports=Password; } module.exports =密码;

MY ROUTING FILE 我的路由文件

seller.post('/change',(req,res)=>{

    console.log('kkkkkkkkkkkkkk');
    res.render('change');
    var newPass=req.body.newPass;
    console.log("New Password:::",newPass);
    var confirmPass=req.body.confirmPass;
    console.log('Confirm Password:::',confirmPass);   
    var passPanel=new password(newPass,confirmPass);
    var pr=operations.findOneAndUpdate(passPanel);

    pr.then(data=>{
        console.log(data);
        res.render('change',{newPass:data.newPass, confirmPass:data.confirmPass});

    })
})


             seller.post('/submit',(req,res)=>{
            res.render('submit');
           });
      module.exports=seller;

MY SERVER CONFIGURATION 我的服务器配置

app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.set("view engine","ejs");
app.use('/', require('./Routes/sellerRoutes'));
app.listen(process.env.PORT||1234, (err)=>{

    if(err) {
        console.log('An Error has occured', err);
        logger.error('Server Not Start ',err);
    }

    else {

        console.log("Server Started");
        logger.debug('Server Started');
    }
})

MY EJS FILE CODE 我的EJS文件代码

<body class="section">
    <h1><center>Password Change</center></h1>
    <form method="POST" action="submit">
        <% var newPass;%> <% var confirmPass; %>
        <label for="">New Password:</label>
        <input type="password" id="newPass" name="newPass" value="<%=newPass%>">

    <div class="cnfrm">
        <label for="">Confirm Password:</label>
        <input type="password" id="confirmPass" name="confirmPass" value="<%=confirmPass%>">
    </div>
        <button id="chngepswd" class="btn btn-success">OK</button></a>
    </form>
    <br>
</body>

When I am coding in node. 当我在节点中编码时。 I made an ejs template in which I've made a form. 我制作了一个ejs模板,在其中制作了表格。 Form is using method POST. 表单使用方法POST。 When I am routing it with POST method it does not render the page and it says CANNOT GET. 当我使用POST方法路由它时,它不会呈现页面,它表示无法获取。

On the other hand, when I am using it with GET request it is absolutely working fine means it is rendering me the ejs page that I've made. 另一方面,当我使用它与GET请求它绝对正常工作意味着它渲染我我做的ejs页面。

I'm not able to understand why my ejs is rendering me the page with GET request and not with POST request. 我无法理解为什么我的ejs使用GET请求呈现我的页面而不是POST请求。

Your action on the form is submit 你的action的形式是提交

<form method="POST" action="submit">

This means the browser will try to access /submit , but you don't seem to have a route for that. 这意味着浏览器将尝试访问/提交 ,但您似乎没有路由。

You probably need to change the action to /change . 您可能需要将操作更改为/更改

In your seller.post method, you are calling res.render 2 times. 在您的seller.post方法中,您要调用res.render 2次。 This will not work because res.render sends HTTP response back to the client and you can't send 2 responses to one request. 这不起作用,因为res.render将HTTP响应发送回客户端,您不能向一个请求发送2个响应。

While res.render doesn't need to be the last statement in your method, it cannot be followed by another piece of code that generates response as one has already been sent. 虽然res.render不需要是您方法中的最后一个语句,但它不能跟随另一段代码生成响应,因为已经发送了一个响应。

You should remove the first res.render in your method. 您应该删除方法中的第一个res.render

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

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