Home > other >  ejs : why it's stop to render CSS?
ejs : why it's stop to render CSS?

Time:09-22

I have a misunderstanding with EJS, when i do a post request, EJS renders my CSS correctly, but when I do this:

exports.resetpassword = async (req, res) => {
  const {id, token} = req.params;
  
  
  const user = await LoginModel.findById(id).exec();
  const secret = process.env.TOKEN_SECRET   user.password
  
  try {
    const payload = jwt.verify(token, secret)
    req.flash('emailReseter', `${user.email}`);
    res.render("resetpassword.ejs", {messageMail: req.flash("emailReseter")});
  } catch (error) {
    console.log(error.message);
  }
  
};

here's what I see:

enter image description here

I can't use my css file and I don't know where the problem comes from.

My others EJS file is the same, other than the head link and my css works correctly.

CodePudding user response:

So far the only way i managed it

i add in my routes:

const router = require("express").Router();
const services = require('../service/render');

router.get("/", services.login);
router.get('/forgot-password', services.forgotpassword);
router.get('/reset-password/:id/:token', services.resetpassword);
router.get('/reset-password', services.resetpasswordredirect)

in my render.js

exports.resetpassword = async (req, res) => {
  const {id, token} = req.params;
  
  
  const user = await LoginModel.findById(id).exec();
  const secret = process.env.TOKEN_SECRET   user.password
  
  try {
    const payload = jwt.verify(token, secret)
    req.flash('emailReseter', `${user.email}`);
    res.redirect('/reset-password')
  } catch (error) {
    console.log(error.message);
  }
  
};

exports.resetpasswordredirect = async (req,res)=>{
  res.render("resetpassword.ejs", {messageMail: req.flash("emailReseter")});
  
}

CodePudding user response:

My previous solution wasn't good because if i had an ID in the params, then my css doesn't work.

So I changed in my server.js

app.use(express.static("public"));

to:

app.use('/public', express.static("public"));

Also changed all my ejs files, for example :

<link rel="stylesheet" href="css/login.css">

became :

<link rel="stylesheet" href="/public/css/login.css">
  • Related