Express.js basics Flashcards
What are the advantages of using Express.js with Node.js?
Simplified web development, middleware support, flexible routing system, template engine integration.
How to create an HTTP server using Express.js?
import {express} from “express”
const app = express();
const PORT = 3000;
app.listen(PORT, ()=>{console.log(“callback function”})
What is middleware in Express.js and when to use them?
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 to implement middleware in Express.js?
- Define a middleware function (accepts 3 parameters: req, res, next)
- Use app.use() to mount the middleware globally, meaning it will be executed for every incoming request to the application.
- Start the server by listening on a specified port using app.listen().
What is the purpose of the app.use() method in Express.js?
Used to mount middleware functions globally.
What is the purpose of the next parameter in Express.js?
It’s a callback function used to pass control to the next middleware function in the stack.
How to use middleware globally for a specific route?
app.use(‘/specific_route’, middleware)
What is the request pipeline in Express?
All the steps that the request goes through until it reaches the endpoint.