Express.js Flashcards

1
Q

What is Express.js?

A

Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for building web and mobile applications.

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

True or False: Express.js is built on top of the Node.js HTTP module.

A

True

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

Fill in the blank: Express.js uses ________ for routing.

A

middleware

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 in Express.js is a function that has access to the request object, the response object, and the next middleware function in the application’s request-response cycle.

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

How do you define a route in Express.js?

A

You define a route in Express.js using app.METHOD(path, handler), where METHOD is the HTTP method (GET, POST, etc.), path is the URL path, and handler is the callback function.

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

What is the purpose of the ‘next’ function in middleware?

A

The ‘next’ function is used to pass control to the next middleware function in the stack. If not called, the request will be left hanging.

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

What are the main advantages of using Express.js?

A

The main advantages of using Express.js include its lightweight nature, flexibility, extensive middleware support, and the ability to create RESTful APIs easily.

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

True or False: Express.js can serve static files.

A

True

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

What is the function of ‘app.use()’ in Express.js?

A

‘app.use()’ is used to mount middleware functions at a specified path. It can be used for serving static files, handling JSON requests, and more.

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

What is the difference between app.get() and app.post()?

A

app.get() is used to handle GET requests, which retrieve data from the server, while app.post() is used to handle POST requests, which send data to the server.

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

How can you handle errors in Express.js?

A

You can handle errors in Express.js by defining an error-handling middleware function with four arguments: err, req, res, and next.

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

What is the purpose of the ‘body-parser’ middleware?

A

The ‘body-parser’ middleware is used to parse incoming request bodies in a middleware before your handlers, available under the req.body property.

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

What does ‘res.send()’ do?

A

‘res.send()’ is used to send a response to the client, and it can send various types of responses, including strings, objects, and buffers.

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

What is a route parameter in Express.js?

A

A route parameter is a placeholder in a route path that captures values from the URL, which can be accessed using req.params.

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

How do you serve static files in Express.js?

A

You can serve static files in Express.js by using the ‘express.static’ middleware, which serves files from a specified directory.

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

What is a RESTful API?

A

A RESTful API is an application programming interface that adheres to the principles of Representational State Transfer (REST), using standard HTTP methods and status codes.

17
Q

What is the purpose of the ‘cors’ middleware?

A

‘cors’ middleware is used to enable Cross-Origin Resource Sharing (CORS) in Express.js, allowing restricted resources on a web page to be requested from another domain.

18
Q

What are the common HTTP status codes used in Express.js?

A

Common HTTP status codes include 200 (OK), 201 (Created), 400 (Bad Request), 401 (Unauthorized), 404 (Not Found), and 500 (Internal Server Error).

19
Q

True or False: Express.js is only suitable for building web applications.

A

False

20
Q

What is the purpose of the ‘express.Router()’ method?

A

The ‘express.Router()’ method creates a new router object that can be used to define routes and middleware for a specific part of an application.

21
Q

How do you set the view engine in an Express.js application?

A

You set the view engine in an Express.js application by using ‘app.set(‘view engine’, ‘engineName’)’, where ‘engineName’ is the name of the templating engine.

22
Q

What is the function of ‘res.json()’?

A

‘res.json()’ is used to send a JSON response to the client, automatically setting the Content-Type header to application/json.

23
Q

How can you define a catch-all route in Express.js?

A

You can define a catch-all route in Express.js by placing a route handler at the end of your route definitions using app.get(‘*’, handler).

24
Q

What is the purpose of ‘app.listen()’?

A

‘app.listen()’ is used to bind and listen for connections on a specified host and port, effectively starting the Express server.