简体   繁体   中英

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;

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

<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. Form is using method POST. When I am routing it with POST method it does not render the page and it says CANNOT GET.

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.

I'm not able to understand why my ejs is rendering me the page with GET request and not with POST request.

Your action on the form is submit

<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. This will not work because res.render sends HTTP response back to the client and you can't send 2 responses to one request.

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.

You should remove the first res.render in your method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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