Re-render Express/Node/EJS

// Requires:
const express = require("express");
const quote = require("inspirational-quotes");
const bodyParser = require("body-parser");

// Usages:
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
let randomQuote = "";
let randomAuthor = "";

// EJS
app.set("view engine", "ejs");

app.get("/", (req, res) => {
  randomQuote = quote.getQuote().text;
  randomAuthor = quote.getQuote().author;
  res.render("index.ejs", {
    randomQuote: randomQuote,
    randomAuthor: randomAuthor,
  randomQuote = "";
  randomAuthor = "";
});"/", (req, res) => {
  randomQuote = quote.getQuote().text;
  randomAuthor = quote.getQuote().author;


app.listen(3000, () => console.log("running"));

Okay… Need help…
When page loads I get what I need…
When I click on the Button I need a NEW quote to be generated.
Any clues?

The button is on the client-side (which isn’t shown here), which can reload the page with reload

This will force refresh the page, and thus re-call the back-end to re-render the quote.

Its worth noting that there are alternative methods for re-loading the quote by using JavaScript on the client-side to call your back-end (or the quote api directly) and updating the DOM. This will provide a better user experience than a full page refresh, but the choice is up to you.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.