res.render. What will the user see in their browser when they visit /welcome?app.get('/welcome', (req, res) => { res.render('welcome', { name: 'Alice' }); }); // Assume 'welcome' template contains: <h1>Hello, <%= name %>!</h1>
name is passed to the template and how it is used inside.The res.render method sends the rendered HTML of the 'welcome' template to the browser. The template uses <%= name %> to insert the value of name passed in the object. Since name is 'Alice', the output is <h1>Hello, Alice!</h1>.
res.render to send a template with data?res.render must be an object with key-value pairs.The res.render method expects the second argument to be an object containing data for the template. Option B correctly passes an object with age: 30. Other options use incorrect syntax.
res.render call cause an error?app.get('/dashboard', (req, res) => {
res.render('dashboard', { user: userData });
});
Assuming userData is not defined anywhere, what error will occur?res.render are declared.The variable userData is used but never declared or assigned. This causes a ReferenceError when the code tries to access it.
/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.
<% } %>loggedIn and how the template uses it in the if statement.The variable loggedIn is false. The template shows the else block, so the output is <p>Please log in.</p>.
res.render in Express is TRUE?res.render works in Express.res.render in an Express route.res.render compiles the template with data, sends the resulting HTML to the client, and ends the response. It works with any configured template engine and templates are usually stored in the views folder, not public.