0
0
Expressframework~20 mins

res.render for templates in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Express Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Express code output in the browser?
Consider this Express route handler using res.render. What will the user see in their browser when they visit /welcome?
Express
app.get('/welcome', (req, res) => {
  res.render('welcome', { name: 'Alice' });
});

// Assume 'welcome' template contains: <h1>Hello, <%= name %>!</h1>
AError: Template not found
B<h1>Hello, name!</h1>
C<h1>Hello, Alice!</h1>
D<h1>Hello, undefined!</h1>
Attempts:
2 left
💡 Hint
Look at how the variable name is passed to the template and how it is used inside.
📝 Syntax
intermediate
1:30remaining
Which option correctly uses res.render to send a template with data?
You want to render the 'profile' template and pass the user's age as 30. Which code snippet is correct?
Ares.render('profile', age=30);
Bres.render('profile', { age: 30 });
Cres.render('profile', ['age', 30]);
Dres.render('profile', age: 30);
Attempts:
2 left
💡 Hint
Remember that the second argument to res.render must be an object with key-value pairs.
🔧 Debug
advanced
2:00remaining
Why does this res.render call cause an error?
Look at this Express route:
app.get('/dashboard', (req, res) => {
  res.render('dashboard', { user: userData });
});
Assuming userData is not defined anywhere, what error will occur?
AReferenceError: userData is not defined
BTypeError: res.render is not a function
CSyntaxError: Unexpected token {
DError: Template 'dashboard' not found
Attempts:
2 left
💡 Hint
Check if all variables used inside res.render are declared.
state_output
advanced
2:00remaining
What is the rendered output of this Express route with conditional data?
Given this route and template, what will the browser display when visiting /status? Route:
app.get('/status', (req, res) => {
  const isLoggedIn = false;
  res.render('status', { loggedIn: isLoggedIn });
});
Template 'status':
<% if (loggedIn) { %>
  

Welcome back!

<% } else { %>

Please log in.

<% } %>
A<p>Welcome back!</p>
BError: loggedIn is not defined
C<p>loggedIn is false</p>
D<p>Please log in.</p>
Attempts:
2 left
💡 Hint
Check the value of loggedIn and how the template uses it in the if statement.
🧠 Conceptual
expert
2:30remaining
Which statement about res.render in Express is TRUE?
Choose the correct statement about how res.render works in Express.
A<code>res.render</code> sends the rendered HTML to the client and ends the response automatically.
B<code>res.render</code> only compiles the template but does not send any response to the client.
C<code>res.render</code> requires the template file to be in the public folder to work.
D<code>res.render</code> can only be used with EJS templates, not with other engines.
Attempts:
2 left
💡 Hint
Think about what happens after calling res.render in an Express route.