I want to be redirected to a file.js after when i am logged in

48 Views Asked by At

This is my Express code by using handlebars using .hbs code

const express = require("express");
const path = require("path"); 
const app = express();
const port = 3001;
//for mongo all mongodata
require("dotenv").config();
const cors = require("cors");
app.use(cors());


    
const Handle = require("./views/mongoschema");

const staticPath= path.join(__dirname, "./views");


app.set("view engine","hbs");


app.use(express.static(staticPath));

//for maintaining the format
app.use(express.json());

//to GET the data in the from the form
app.use(express.urlencoded({extended:false}));


//to render the page this is coming (1st)
app.get("/",(req, res) => {
    res.render('index'); 
    });


//for login validation
app.post("/mongoschema",async(req, res) => {
    try {
        const handleData = new Handle({
            title: req.body.one,
            description: req.body.two,
            url: req.body.four
        })

       const handled = await handleData.save();
       //After REGISTRATION sending the user to index file
       ***res.status(201).render("index");***
       
    } catch (error) {
        res.status(400).send(error);
    }
    });


  app.get("/pages", (req, res) => {
      Handle.find({})
        .then((items) => res.json(items))
        .catch((err) => console.log(err));
    });



app.listen(port, () => {
    console.log('listening to the port ${port)');
 });

the file i want to run now is a "file.js" and index is "index.hbs" it is not able to render the "file.js" how can i render or i will be redirected to the "file.js" file After when my login is SUCCESSFUL.

1

There are 1 best solutions below

0
On

As someone mentioned in the comments, you should be using:

res.redirect("/"); // path to route as parameter - in this case, index.