Express.js basics Flashcards

1
Q

What are the advantages of using Express.js with Node.js?

A

Simplified web development, middleware support, flexible routing system, template engine integration.

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

How to create an HTTP server using Express.js?

A

import {express} from “express”

const app = express();
const PORT = 3000;

app.listen(PORT, ()=>{console.log(“callback function”})

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

What is middleware in Express.js and when to use them?

A

Middleware is a function that sits between the client and the application endpoints (get, post, put, delete, etc). It performs operations and passes control to the next middleware.

Request from client -> Pipeline of middlewares (logging, authentication, cors, etc) -> endpoint
Endpoint -> Response to client

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

How to implement middleware in Express.js?

A
  1. Define a middleware function (accepts 3 parameters: req, res, next)
  2. Use app.use() to mount the middleware globally, meaning it will be executed for every incoming request to the application.
  3. Start the server by listening on a specified port using app.listen().
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the purpose of the app.use() method in Express.js?

A

Used to mount middleware functions globally.

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

What is the purpose of the next parameter in Express.js?

A

It’s a callback function used to pass control to the next middleware function in the stack.

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

How to use middleware globally for a specific route?

A

app.use(‘/specific_route’, middleware)

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

What is the request pipeline in Express?

A

All the steps that the request goes through until it reaches the endpoint.

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