Company logo
  • Jobs
  • Bootcamp
  • About Us
  • For professionals
    • Home
    • Jobs
    • Courses
    • Questions
    • Teachers
    • Bootcamp
  • For business
    • Home
    • Our process
    • Plans
    • Assessments
    • Payroll
    • Blog
    • Calculator

0

123
Views
How to always pass 'render' parameters in Express

I'm building a website where some parameters need to be passed to the frontend for every page that's loaded (ie text that's used in a navbar that is on everypage). At the moment I'm just including all the parameters in every render line but I'm wondering if there's a more efficient way to do it (in middleware for example). The two parameters below are being used in every render.

return res.render('index', {
    docTitle: i18n.__('home'),
    isLoggedIn: req.session.isLoggedIn,
})
8 months ago · Santiago Trujillo
1 answers
Answer question

0

You could create your own render function, where you set the default values and delegate to res.render:

const customResRender = (req, res, page, params = {}) => 
  res.render(page, {
    docTitle: i18n.__('home'),
    isLoggedIn: req.session.isLoggedIn,
    ...params
  });

Then, use it instead of res.render:

app.get("/someHandler", (req, res) => {
  // ...
  customResRender(req, res, "index");
});
8 months ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post job Plans Our process Sales
Legal
Terms and conditions Privacy policy
© 2023 PeakU Inc. All Rights Reserved.