Express.js & Web APIs Flashcards

Express Basics (Routing, Middleware) REST APIs Request Lifecycle Error Handling Authentication & Authorization (43 cards)

1
Q

What is Express.js?

A

Express.js is a fast, unopinionated, minimalist web framework for Node.js used to build web and API applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are routes in Express.js?

A

Routes in Express.js define how the application responds to a client request for a particular endpoint and HTTP method.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you define a GET route in Express.js?

A

app.get(‘/route’, (req, res) => { res.send(‘response’); });

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is middleware in Express.js?

A

Middleware is a function that has access to the request, response, and next function in the request-response cycle and can modify them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are common types of middleware in Express?

A

Application-level, Router-level, Built-in, and Error-handling middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the purpose of REST APIs?

A

REST APIs expose resources using standard HTTP methods, enabling stateless communication between client and server.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the lifecycle of a request in Express?

A

Request → Middleware stack → Route handler → Response → Optional error handling middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How does Express handle errors?

A

By passing an error to next(err), Express will skip remaining middleware and move to the error-handling middleware.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is authentication in Express.js?

A

Authentication verifies a user’s identity using credentials like username/password or tokens.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is authorization in Express.js?

A

Authorization determines what an authenticated user is allowed to do (e.g., access resources or endpoints).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What’s the difference between authentication and authorization?

A

Authentication verifies who the user is; authorization verifies what the user has access to.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How can you implement JWT-based authentication in Express?

A

By signing a token during login and verifying it in a middleware using a library like jsonwebtoken.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are the advantages of Express.js?

A

Simple to use, large ecosystem, flexible, supports middleware, and excellent community support.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the disadvantages of Express.js?

A

Less structured, requires manual error handling, lacks built-in advanced features, and can get messy with large codebases.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is a best practice for structuring Express apps?

A

Use MVC or modular folder structure, separate routes/controllers/middleware, and apply error handling globally.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s a common use case for Express.js?

A

Building REST APIs, backend for web or mobile apps, or proxy servers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How does Express.js impact system design?

A

It promotes modular and scalable design, supports middleware chaining, and allows easy integration with databases and services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Give an example of middleware usage.

A

app.use(express.json()); parses incoming JSON requests.

19
Q

Give an example of using async/await in route handlers.

A

app.get(‘/data’, async (req, res) => { const result = await db.query(); res.send(result); });

20
Q

What are the architectural implications of using middleware?

A

Middleware encourages a pipeline architecture where requests pass through layered processing logic.

21
Q

How can you make Express apps fault-tolerant?

A

Use global error handling, process monitoring (e.g., PM2), retries, and validation.

22
Q

How do you monitor Express applications?

A

Using tools like morgan, winston, Prometheus, Grafana, or integrating APMs like New Relic or Datadog.

23
Q

How can you debug an Express.js app?

A

Use logging (e.g., console.log, winston), the Node.js debugger, or VS Code debugging tools.

24
Q

What are tradeoffs of using Express over a full-fledged framework?

A

Express offers flexibility but lacks conventions and features, requiring more setup for larger applications.

25
What is a common gotcha with asynchronous error handling in Express?
For async route handlers, unhandled errors won't reach error middleware unless wrapped or using async middleware libraries.
26
Why is input validation important in Express apps?
To prevent security issues like injection attacks and ensure the integrity of incoming data.
27
Which library is commonly used for validation in Express?
Joi, express-validator, or Zod are commonly used libraries.
28
What is a common mistake when using middleware?
Not calling `next()`, which causes requests to hang indefinitely.
29
What’s the difference between `app.use()` and `app.get()`?
`app.use()` applies middleware to all HTTP methods, `app.get()` handles only GET requests at the specified path.
30
Why use Helmet in Express?
Helmet helps secure Express apps by setting various HTTP headers.
31
What’s the benefit of CORS middleware in Express?
It allows or restricts cross-origin requests to your Express API.
32
What is an interview question about Express routing?
How does Express match route handlers and what happens if no route matches?
33
What is a potential security risk with Express?
Not sanitizing user input or exposing sensitive error messages.
34
How can Express.js scale horizontally?
By running multiple instances behind a load balancer using clustering or PM2.
35
What is an example of route-level middleware?
`router.get('/user', authMiddleware, getUserHandler);`
36
What is a 404 handler in Express?
A middleware that handles requests with no matching route: `app.use((req, res) => res.status(404).send('Not Found'))`
37
What’s the role of `next()` in middleware?
It passes control to the next middleware in the stack.
38
What does `express.json()` do?
It parses incoming JSON payloads and makes them available on `req.body`.
39
What is the role of body-parser in Express?
`body-parser` parses incoming request bodies; it's now included in Express as `express.json()` and `express.urlencoded()`.
40
What is the impact of poor error handling in Express?
It can cause app crashes, unhandled promise rejections, or expose internal logic to clients.
41
What are common interview questions about Express?
Explain middleware, how Express handles errors, or how you structure an Express app.
42
What should you log in a production Express app?
Request info, errors, timestamps, user IDs, and trace IDs for correlation.
43
How does Express compare to Fastify or Koa?
Express is more popular and flexible, while Fastify offers better performance and Koa focuses on modern async patterns.