I have an application using NodeJS, Express (and a few more dependencies). I reduced the application to the file below to explain my question:
app.js:
// Dependências.
const express = require('express');
// Criar a instância do express.
let app = express();
// Middlewares.
app.set('view engine', 'ejs');
app.set('views', './public/views');
app.use('/assets', express.static('./public/assets'));
// Rotas.
app.get('/', function (req, res) {
res.render('index', { title: 'Título' });
});
app.get('/users', function (req, res) {
res.render('users', { title: 'Título' });
});
app.get('/groups', function (req, res) {
res.render('groups', { title: 'Título' });
});
app.get('/calendar', function (req, res) {
res.render('calendar', { title: 'Título' });
});
// Iniciar o servidor.
const port = process.env.PORT || 80;
app.listen(port, function () {
console.log('Server listening at port ${port}.');
});
Note that in all app.get
, I passed a variable to the view. This variable is: title
.
Is there a way to always pass a variable to views without necessarily putting them in the second parameter of the render()
function?
Thank you.