Is there any way to use EJS variables with the express "redirect" method instead of render?

Asked

Viewed 9 times

0

Follow the example of an html page (index.ejs) with a message written "Password check" and a form with an input and a button. I want that, when filling this input with a password and making the request, it is redirected to the same page, but with the message modified to "Correct password" or "Wrong password", depending on what is typed. The problem is that these variables can only be sent by the render method, not by redirect.

The correct password is "123".

The packages needed to run are: express, ejs.

Index ejs.:

 <form action="/verify" method="POST">
   <input type="password" name="password">
   <button>Enviar</button>
 </form>
 <p>
   <%=passStatus%>
 </p>

Index.js:

const express = require("express");
const app = express();
const path = require("path");
app.use(express.urlencoded({ extended: true }));

// EJS Settings
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));

// Routes
app.get("/", (req, res) => {
  res.render("index", { passStatus: "Verificar senha" });
});

app.post("/verify", (req, res) => {
  let corectPass = 123;
  let verifyPass = req.body.password;
  if (corectPass == verifyPass) {
    res.redirect("/");
    // Pensei em algo assim ↓, mas a "passStatus" só funciona com o método ".render".
    // res.redirect("/", {passStatus: "Senha correta"});
  } else {
    res.redirect("/");
    // Pensei em algo assim ↓, mas a "passStatus" só funciona com o método ".render".
    // res.redirect("/", {passStatus: "Senha errada"});
  }
});

app.listen(5000, () => {
  console.log("Servidor Rodando na porta 5000");
});

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.