Complete the code to send data to the template using Express.
app.get('/', (req, res) => { res.render('index', { title: [1] }); });
We pass the data as an object with keys and values. Here, the key is title and the value is the string 'Home Page'.
Complete the code to pass a list of users to the template.
app.get('/users', (req, res) => { const users = ['Alice', 'Bob', 'Charlie']; res.render('users', { [1]: users }); });
The key name in the object should match what the template expects. Here, users is the key used to pass the list.
Fix the error in passing multiple data items to the template.
app.get('/profile', (req, res) => { const name = 'John'; const age = 30; res.render('profile', { name: name, [1] }); });
When passing multiple data items, each must be a key-value pair separated by commas. The correct syntax is age: age.
Fill both blanks to pass a title and a list of items to the template.
app.get('/items', (req, res) => { const items = ['Pen', 'Notebook', 'Eraser']; res.render('items', { [1]: 'My Items', [2]: items }); });
The object keys should be title for the string and items for the list to match the template variables.
Fill all three blanks to pass user info and a flag to the template.
app.get('/dashboard', (req, res) => { const user = { name: 'Anna', role: 'admin' }; const isLoggedIn = true; res.render('dashboard', { [1]: user.name, [2]: user.role, [3]: isLoggedIn }); });
The keys username, role, and loggedIn are used to pass the user's name, role, and login status respectively to the template.