install package
The app.set() method in Express is used to store application-level settings and configuration values. These settings can be used throughout the application. Here are some real-world examples of how app.set() is used:
1. Setting the View Engine
app.set() is commonly used to configure the template engine for rendering dynamic views.
const express = require('express');
const app = express();
// Set the view engine to EJS
app.set('view engine', 'ejs');
// Set the directory for views (remove if using default views folder)
app.set('views', './views');
app.get('/', (req, res) => {
res.render('index', { title: 'Home Page' });
});
view engine: Specifies the template engine (e.g., EJS, Pug, Handlebars). : Specifies the directory where view files are located. 2. Working with partials, includes
Working with default meta header
What is res.locals?
res.locals is an object in Express that is used to store data that you want to make available to the current response and its associated view templates. It is specific to the current request-response cycle.
How res.locals Works
Scope: The data stored in res.locals is available only for the current request and is not shared across requests. Purpose: It is commonly used to pass data to templates rendered with res.render(). app.use((req, res, next) => {
res.locals.pageTitle = 'Default Title';
next();
});
app.get('/', (req, res) => {
res.render('index'); // The template can access `pageTitle`
});
What is locals?
locals is a property available on both res (response) and app (application). res.locals: Used for data specific to the current request-response cycle. app.locals: Used for data that is shared across all requests in the application. app.locals.siteName = 'MyApp';
app.get('/', (req, res) => {
res.render('index'); // `siteName` is available in all templates
});
Use with res.locals for pageTitle because it is request-specific and can vary dynamically. Use app.locals for global constants that remain the same across all requests. This is ideal for data like the site name, version, or other constants that don't vary per request. Controllers
Models
Create a new model
The difference between a static function and a regular function inside a class lies in how they are called and what they operate on:
1. Static Function
Definition: A static function is defined using the static keyword inside a class. Access: It is called directly on the class itself, not on an instance of the class. Purpose: Typically used for utility functions or operations that do not depend on instance-specific data. Example:
class Article {
static fetchAll() {
return ['Article 1', 'Article 2'];
}
}
// Call the static method directly on the class
console.log(Article.fetchAll()); // Output: ['Article 1', 'Article 2']
Static methods cannot access instance-specific properties (this). They are useful for operations that are shared across all instances or are independent of any instance. 2. Regular Function (Instance Method)
Definition: A regular function is defined without the static keyword. Access: It is called on an instance of the class. Purpose: Used for operations that depend on the specific instance's data. Example:
class Article {
constructor(title) {
this.title = title;
}
save() {
console.log(`Saving article: ${this.title}`);
}
}
// Create an instance of the class
const article = new Article('My First Article');
article.save(); // Output: Saving article: My First Article
Regular methods can access instance-specific properties using this. They are tied to the specific instance of the class.