📖 Scenario: You are building a simple web server using Express.js. You want to show a welcome page to users using a template.
🎯 Goal: Create an Express app that uses res.render to display a template called welcome with a message.
📋 What You'll Learn
Create an Express app instance called app
Set the view engine to pug
Create a route for / that uses res.render to show the welcome template
Pass a variable called message with the value 'Hello, visitor!' to the template
💡 Why This Matters
🌍 Real World
Web servers often use templates to create dynamic HTML pages that show different content depending on the user or situation.
💼 Career
Knowing how to use res.render and templates is essential for backend web developers working with Express.js to build user-friendly websites.
Progress0 / 4 steps
1
Set up Express app and import express
Write code to import express and create an Express app instance called app.
Express
Hint
Use require('express') to import Express and call it to create app.
2
Set the view engine to pug
Add code to set the view engine of app to 'pug' using app.set.
Express
Hint
Use app.set('view engine', 'pug') to tell Express to use Pug templates.
3
Create a route that renders the welcome template
Write a route handler for GET / on app that calls res.render to render the template named 'welcome' and passes an object with message set to 'Hello, visitor!'.
A. res.render cannot be used inside app.get routes.
B. The template name 'about' should be a variable, not a string.
C. The second argument to res.render should be an object, but it's missing curly braces.
D. The route path '/about' is invalid.
Solution
Step 1: Check the syntax of res.render arguments
The second argument must be an object with key-value pairs, but here it's written without curly braces.
Step 2: Identify the correct object syntax
It should be { title: 'About Us' } to pass variables properly.
Final Answer:
The second argument to res.render should be an object, but it's missing curly braces. -> Option C
Quick Check:
res.render second argument = object with braces [OK]
Hint: Always wrap variables in an object with curly braces for res.render [OK]
Common Mistakes:
Omitting curly braces around variables object
Passing variables as separate arguments
Misunderstanding route handler usage
5. You want to render a 'profile' template that shows a user's name and age. The user data is in an object user = { name: 'Bob', age: 30 }. Which code correctly renders the template passing the user data so the template can access name and age directly?
hard
A. res.render('profile', ['name', user.name, 'age', user.age]);
B. res.render('profile', { user });
C. res.render('profile', { name: user, age: user.age });
D. res.render('profile', { name: user.name, age: user.age });
Solution
Step 1: Understand how template variables are accessed
The template expects variables name and age directly, not inside a nested object.
Step 2: Pass variables as separate keys in an object
Passing { name: user.name, age: user.age } makes name and age available directly in the template.
Final Answer:
res.render('profile', { name: user.name, age: user.age }); -> Option D
Quick Check:
Pass variables as keys for direct template access [OK]
Hint: Pass each variable separately for direct template use [OK]
Common Mistakes:
Passing the whole user object instead of separate keys
Passing variables as an array instead of object
Assuming template can access nested object keys without dot notation
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.