res.render helps you show web pages using templates. It fills in data and sends the final page to the browser.
res.render for templates in Express
res.render(view, [locals], callback)
view is the template file name without extension.
locals is an optional object with data to fill in the template.
res.render('index')res.render('profile', { name: 'Alice', age: 30 })
res.render('error', { message: 'Page not found' }, (err, html) => { if (err) console.error(err); else res.send(html); })
This Express app uses res.render to show the 'index' template. It sends a title and message to fill the page.
import express from 'express'; const app = express(); app.set('view engine', 'pug'); app.get('/', (req, res) => { res.render('index', { title: 'Home Page', message: 'Welcome to our site!' }); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Make sure to set the template engine with app.set('view engine', 'yourEngine') before using res.render.
Templates usually live in a folder named views in your project root.
If you pass data in locals, you can use it inside your template to show dynamic content.
res.render creates a web page from a template and data.
Use it to send dynamic HTML pages to users.
Remember to set your template engine and keep templates in the views folder.
Practice
res.render do in an Express app?Solution
Step 1: Understand the purpose of res.render
res.renderis used to create a web page by combining a template with data.Step 2: Identify what res.render sends to the client
It sends the generated HTML page as the response to the user's browser.Final Answer:
It creates and sends an HTML page using a template and data. -> Option AQuick Check:
res.render = sends HTML page [OK]
- Confusing res.render with res.json
- Thinking res.render redirects the user
- Assuming res.render logs info instead of sending response
Solution
Step 1: Check the method signature of res.render
It takes the template name as first argument and an object with variables as second argument.Step 2: Match the correct object syntax
The correct way to pass variables is using an object like { title: 'Welcome' }.Final Answer:
res.render('home', { title: 'Welcome' }); -> Option BQuick Check:
res.render(template, object) = correct syntax [OK]
- Passing variables as separate arguments instead of an object
- Using array instead of object for variables
- Assigning variables inside the argument list incorrectly
app.get('/', (req, res) => {
res.render('index', { name: 'Alice' });
});
Assuming the 'index' template contains: <h1>Hello, <%= name %>!</h1>Solution
Step 1: Understand how res.render passes data to template
The object { name: 'Alice' } provides the value fornamein the template.Step 2: Evaluate the template output
The template uses<%= name %>to insert the value 'Alice' into the HTML.Final Answer:
<h1>Hello, Alice!</h1> -> Option AQuick Check:
Template variable replaced correctly =Hello, Alice!
[OK]
- Expecting raw template tags to appear in output
- Assuming missing template causes silent empty output
- Confusing template syntax with HTML escaping
app.get('/about', (req, res) => {
res.render('about', title: 'About Us');
});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 CQuick Check:
res.render second argument = object with braces [OK]
- Omitting curly braces around variables object
- Passing variables as separate arguments
- Misunderstanding route handler usage
user = { name: 'Bob', age: 30 }. Which code correctly renders the template passing the user data so the template can access name and age directly?Solution
Step 1: Understand how template variables are accessed
The template expects variablesnameandagedirectly, not inside a nested object.Step 2: Pass variables as separate keys in an object
Passing{ name: user.name, age: user.age }makesnameandageavailable directly in the template.Final Answer:
res.render('profile', { name: user.name, age: user.age }); -> Option DQuick Check:
Pass variables as keys for direct template access [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
