Custom template location
Store templates outside the conventional project directory.
Express.js · Const
The views application setting identifies the directory, or ordered directories, that Express uses to find view templates. It defaults to process.cwd() + '/views'.
The views application setting tells Express where templates are stored. It accepts one directory or an ordered list of directories; rendering searches that list in order, and its default is a views directory under the current working directory.
app.set('views', path)
app.get('views')| Name | Type | Description |
|---|---|---|
name | String | Application setting name, which must be views when reading or configuring this setting. |
The configured value of the views application setting when read with app.get('views').
String | Array, and the default path is based on the current working directory.views value, so set it explicitly when a sub-app needs a different template directory.const express = require('express');
const path = require('path');
const app = express();
app.set('views', [
path.join(__dirname, 'views'),
path.join(__dirname, 'shared-views')
]);
app.get('/', (req, res) => res.render('index'));
app.listen(3000);Configures two view directories; Express searches them in array order when resolving a template.
Store templates outside the conventional project directory.
Combine project templates with reusable templates from another directory.
Control which duplicate template name takes precedence.
views setting control?views is an Express application setting containing either a string directory or an array of directories. Its default is process.cwd() + '/views'.
Follow-up: What happens with an array? Express checks directories in their listed order and uses the first matching view.
views setting have?The setting accepts String | Array. A single string identifies one view directory; an array provides multiple lookup directories.
Follow-up: Why might an array be useful? It can combine application-specific templates with shared templates while preserving deterministic precedence.
Express resolves templates against the configured view directories. When an array is used, lookup follows array order, so directory ordering affects which template is selected.
Follow-up: What related setting determines an extension when a rendered view omits one? view engine supplies the default template-engine extension.
app.set()app.get()view engineres.render()