Express.js Flashcards
What is Express.js?
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.
True or False: Express.js is built on top of the Node.js HTTP module.
True
Fill in the blank: Express.js uses ________ for routing.
middleware
What is middleware in Express.js?
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 do you define a route in Express.js?
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.
What is the purpose of the ‘next’ function in middleware?
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.
What are the main advantages of using Express.js?
The main advantages of using Express.js include its lightweight nature, flexibility, extensive middleware support, and the ability to create RESTful APIs easily.
True or False: Express.js can serve static files.
True
What is the function of ‘app.use()’ in Express.js?
‘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.
What is the difference between app.get() and app.post()?
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 can you handle errors in Express.js?
You can handle errors in Express.js by defining an error-handling middleware function with four arguments: err, req, res, and next.
What is the purpose of the ‘body-parser’ middleware?
The ‘body-parser’ middleware is used to parse incoming request bodies in a middleware before your handlers, available under the req.body property.
What does ‘res.send()’ do?
‘res.send()’ is used to send a response to the client, and it can send various types of responses, including strings, objects, and buffers.
What is a route parameter in Express.js?
A route parameter is a placeholder in a route path that captures values from the URL, which can be accessed using req.params.
How do you serve static files in Express.js?
You can serve static files in Express.js by using the ‘express.static’ middleware, which serves files from a specified directory.