Advanced Node and Express Flashcards
What is a template engine?
software tool that simplifies the process of generating dynamic content for web applications. It allows developers to create templates, which are essentially static HTML files with embedded placeholders for dynamic content. These placeholders can be replaced with actual data during runtime, enabling the creation of web pages that can adapt to different data sources or user inputs.
What the first step in setting up a template engine?
npm install ‘template package’
After installing the template engine, in your Express application, how would you configure it to use the template engine as the view engine?
// Set EJS as the view engine
app.set(‘view engine’, ‘ejs’);
After setting the view engine, how do you set the directory for your views?
app.set(‘views’, ‘views’);
Where do you create template engine files?
in the directory specified by ‘app.set(‘views’, ‘views’), depending where you’ve set it to look in ex. views, create a file it that directory.
After creating the views, where and how do you render the views?
-inside the route handlers
-by using the res.render()
What is res.render()
method in Express is used to render a view/template and send the rendered HTML to the client in response to an HTTP request.
What is the syntax for res.render()?
and explain what each part does:
res.render(view, locals, callback)
-views= name of the view/template file to render
-locals= object containing local variables that will be passed to the view for rendering
-callback= callback function that will be called ionce the view rendering is complete
Show an ex. on using res.render() for a ‘hello.ejs’ view, and also containing local variable ‘name’ being ‘John’:
app.get(‘/hello’, (req, res) => {
// Render the ‘hello.ejs’ view
res.render(‘hello’, { name: ‘John’ });
});
What is passport.js?
is a popular authentication middleware for Node.js applications ,that supports various authentication strategies, such as username and password, OAuth, and OpenID.
What various authentication strategies does passport.js support?
username and password, OAuth, and OpenID.
What is the main passport.js module?
passport
What is passport.js strategy for authenticating with a username and password?
passport-local
What is express middleware formanaging sessions?
express-session
What is library for hashing passwords securely?
bcrypt
What is passport?
an authentication middleware for Node.js. It is designed to serve a singular purpose—authenticate requests—and it does so in a modular way that allows you to apply different authentication mechanisms, known as strategies.
What is passport-local?
a module that implements a local authentication strategy for Passport. The “local” part means that it uses a username (or email) and password for authentication.
What is express-session?
middleware for Express that handles sessions. It provides session management capabilities and is essential for maintaining a persistent state across multiple pages, which is useful for keeping a user logged in as they navigate your application.
What is bcrypt?
a library used to hash passwords. bcrypt provides a way to safely store and compare hashed passwords.
How do you configure express-session middleware?
app.use(session({
secret: process.env.SESSION_SECRET, // Change this to a random string
resave: false,
saveUninitialized: false
}));
How do you inialize passportand session support?
app.use(passport.initialize());
app.use(passport.session());
Show a middleware for parsing incoming bodies:
app.use(express.urlencoded({ extended: true }));
What is serialization of a user object?
is an essential aspect of authentication systems, particularly in web applications. It involves converting a user object into a format suitable for storage or transmission, typically for the purpose of session management.
What is often used to store user information in a session after successful authentication and retrieves it on a subsequent requests to identify the authenticated user?
serialization
Explain how serialization typically works in such systems:
After a user is authenticated, their user object is serialized. This typically involves extracting a unique identifier (such as the user’s ID) from the user object and storing it in the session. The serialized user object is then stored in the session store (e.g., memory, database).
Explain how deserialization typically wrks in such systems:
On subsequent requests, the session middleware retrieves the serialized user object from the session store using the unique identifier stored during serialization. This serialized user object is then deserialized, typically by fetching the complete user object from the database based on the unique identifier.
What happens to the complete user object once it is deserialized?
its attached to the request object (often as req.user)
Show an example on how you would serialize the user object:
passport.serializeUser((user, done) => {
done(null, user.id); // Serialize by user ID
});
Show an exaple on how you would deserialize the user object:
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user); // Deserialize by fetching user from database
});
});
What does passport.serializeUser do?
serializes the user object by extracting and storing its ID.
What does passport.deserializeUser do?
deserializes the user object by fetching the complete user object from the database using the stored ID.