How to redirect to previous route with data from current route in express?

Viewed 696

I have a PC build helper website. On homepage there is a list of categories. I click on "Choose" button for a category and it takes me to a page where there are list of products of that category. I select a product by clicking and it should take me to my home page back, and the name of the product should be printed alongside that category.

I am doing this using node js, express, and ejs. My home page has a default "/" route(accessed by typing localhost:3000). When I click choose, it goes to "/product" route(localhost:3000/product), and the name of category is on top of that page. I click on select on a product, it should take me back to homepage on route "/", show the name of product alongside the category.

I am not being able to do this. I cannot pass data from "/product" route by using res.redirect() to my "/" route or homepage. What I did instead, I rendered my homepage onto "/product" route. Thus I am being able to use that data in this route. I wanna be able to redirect to my home route with the data selected from "/product" route and wanna use that data into home route.

I have searched references such as "How to pass data to previous route using express and node js" but didn't find anything helpful for my project.

const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");

const app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/productDB", {useNewUrlParser: true});

//Database Part Start

const listProcessor = new mongoose.Schema({
    name: String,
    core: Number,
    thread: Number
});

const ListProcessor = mongoose.model("ListProcessor", listProcessor);

const item1 = new ListProcessor({
    name: "AMD Ryzen 5 3600",
    core: 6,
    thread: 12,
    price: 17100
});
const item2 = new ListProcessor({
    name: "AMD Ryzen 7 3700x",
    core: 8,
    thread: 12,
    price: 30000
})

const processorArray = [item1, item2];

//Database Part end
//server part and get post part start

app.get("/", function(req, res){
    res.render("buildown", {name: "Not Specified", price: "Not Specified"});
});

app.post("/", function(req, res){
    const signal = req.body.choose;
    if(signal === "cpu")
    {
        ListProcessor.find({}, function(err, items){
            if(items.length === 0)
            {
                ListProcessor.insertMany(processorArray);
                res.redirect("/product");
            }
            else{
                res.redirect("/product");
            }
        });

    }
});

app.get("/product", function(req, res){
    res.render("list", {listTitle: "Processor", listOfProduct: processorArray}); //Using EJS passing data from processorArray
});
app.post("/product", function(req, res){
    const nameItem = req.body.select;
    ListProcessor.find({nameItem}, function(err, items){
        console.log(items);
        res.render("buildown", {name: nameItem, price: items.price});
    });
});

app.listen(3000, function(){
    console.log("Server Up and Running");
});

//Get Post part end

This code first renders homepage to "/" route, with the categories, and Not Specified" text alongside categories. After clicking on choose button, redirects me to "/product" route with a list from data of the mongo DB database I created here. After clicking "Select" it renders the homepage into this "/product" route with the data and product's name in place of "Not Specified" text.

This is all okay but I don't wanna render homepage in this route. Instead this whole action will take place on "/" route. So I would select data from "/products" route, immediately it will redirect to "/" route and I will have the data selected from "/product" route and will use it here.

Can you give me a clue on how to do that? Using just express and node js.

0 Answers
Related