Express JS Express Template Engines 1 — Questions and Answers
Question 1: Which app.set() key is used to configure the default template engine in Express?
- "view engine" (Correct answer)
- "template engine"
- "engine"
- "render engine"
Correct answer: "view engine"
app.set('view engine', 'ejs') tells Express which template engine to use by default for res.render() calls.
Question 2: What directory does Express look for template files in by default?
- /public
- /templates
- /views (Correct answer)
- /assets
Correct answer: /views
Express defaults to a 'views' directory relative to the project root for locating template files.
Question 3: Which method in Express renders a template file and sends the resulting HTML response to the client?
- res.send()
- res.template()
- res.view()
- res.render() (Correct answer)
Correct answer: res.render()
res.render() takes a view name and optional data object, compiles the template, and sends the HTML to the client.
Question 4: In EJS, which tag outputs an HTML-escaped variable value?
- <% var %>
- <%= var %> (Correct answer)
- <%- var %>
- <%# var %>
Correct answer: <%= var %>
<%= var %> escapes the value to prevent XSS, while <%- outputs raw unescaped HTML.
Question 5: How do you pass dynamic data to a template when calling res.render()?
- res.render('view', { key: value }) (Correct answer)
- res.data = { key: value }; res.render('view')
- req.locals = { key: value }; res.render('view')
- app.data({ key: value }); res.render('view')
Correct answer: res.render('view', { key: value })
res.render() accepts a view name as the first argument and an optional plain object of local variables as the second argument.
Question 6: Which of the following is a valid Express-compatible template engine?
- Jinja2
- Django Templates
- Pug (Correct answer)
- Blade
Correct answer: Pug
Pug (formerly Jade) is a popular Node.js template engine with first-class Express support; the others belong to Python/PHP ecosystems.
Question 7: What does app.locals do in the context of Express templates?
- Sets the views directory path
- Defines variables accessible to all templates across all requests (Correct answer)
- Registers a custom template engine
- Enables view caching in production
Correct answer: Defines variables accessible to all templates across all requests
app.locals properties are merged into res.locals for every request, making them available as variables in every rendered template.
Which app.set() key is used to configure the default template engine in Express?