Q1. How do you serve dynamic content in Express?
Dynamic content is generated on the server, often using templating engines.
You fetch data from a database or external API, then pass it to a template with
Alternatively, you can send JSON responses for client-side rendering (SPAs).
You fetch data from a database or external API, then pass it to a template with
res.render().Alternatively, you can send JSON responses for client-side rendering (SPAs).
Q2. What is the difference between server-side and client-side rendering?
Server-side rendering (SSR) generates HTML on the server for each request (using templates).
Client-side rendering (CSR) sends a minimal HTML and JavaScript, and the client builds the UI.
Express can do both: SSR with templates, or serve API data for CSR.
Client-side rendering (CSR) sends a minimal HTML and JavaScript, and the client builds the UI.
Express can do both: SSR with templates, or serve API data for CSR.
Q3. How do you send dynamic JSON responses?
Use
Example:
This is common for REST APIs where the frontend handles rendering.
res.json() with JavaScript objects.Example:
app.get('/api/users', (req, res) => { const users = [{id:1, name:'John'}]; res.json(users); });This is common for REST APIs where the frontend handles rendering.
Q4. How do you combine static and dynamic content?
You can have static files (CSS, images) served via
In your EJS file, you reference static files:
express.static, while routes generate dynamic HTML with templates.In your EJS file, you reference static files:
.Q5. What is res.locals used for?
Data placed here is available to templates rendered during that request-response cycle.
It's useful for passing middleware data (like authenticated user) to views.
res.locals is an object that contains response-local variables scoped to the request.Data placed here is available to templates rendered during that request-response cycle.
It's useful for passing middleware data (like authenticated user) to views.
