Express
30 Questions
Practice Problems
Express Interview Questions
30 Questions
Express.js Basics
What is Express.js?
Express.js is a lightweight and flexible web application framework built on Node.js. It simplifies backend development by providing routing, middleware support, request handling, and API creation features. Developers commonly use Express.js to build REST APIs and server-side applications quickly. Example: const express = require("express"); const app = express();
What are the features of Express.js?
Express.js provides routing, middleware support, template engines, REST API creation, static file serving, and simplified request-response handling. It improves backend development speed and reduces boilerplate code, making it one of the most popular frameworks for Node.js applications. Example: app.use(express.json());
Why is Express.js used with Node.js?
Node.js provides the runtime environment, while Express.js simplifies server-side application development. Express handles routing, middleware, and HTTP requests efficiently, reducing manual coding effort and making backend development faster and more organized. Example: app.get("/", (req, res) => { res.send("Home"); });
What is middleware in Express.js?
Middleware functions execute between receiving a request and sending a response. They can modify requests, validate users, log data, or handle errors. Middleware improves code organization and reusability in Express.js applications. Example: app.use((req, res, next) => { next(); });
What is the difference between Node.js and Express.js?
Node.js is a runtime environment for executing JavaScript outside the browser, while Express.js is a framework built on Node.js. Express simplifies backend development by providing routing, middleware, and request handling features that Node.js alone does not provide directly. Example: const express = require("express");
How do you install Express.js?
Express.js is installed using npm, the Node Package Manager. Developers first initialize a Node.js project and then install Express as a dependency using the npm install command. Example: npm install express
What is the purpose of app.listen()?
app.listen() starts the Express server and makes it listen for incoming client requests on a specified port number. Without this method, the server application will not run or respond to requests. Example: app.listen(3000);
What is routing in Express.js?
Routing defines how an application responds to client requests for specific URLs and HTTP methods. Express.js uses methods like app.get() and app.post() to manage routes and handle different application endpoints. Example: app.get("/about", (req, res) => {});
What is the difference between app.get() and app.post()?
app.get() handles HTTP GET requests used for retrieving data, while app.post() handles POST requests used for sending or creating data on the server. Both methods are commonly used in REST API development. Example: app.get("/users"); app.post("/users");
What is req and res in Express.js?
req represents the incoming client request and contains request data like parameters, body, and headers. res represents the server response used to send data, status codes, or JSON back to the client. Example: app.get("/", (req, res) => { res.send("Hello"); });
Middleware & Routing
What are different types of middleware in Express.js?
Express.js supports application-level middleware, router-level middleware, built-in middleware, error-handling middleware, and third-party middleware. Each type handles specific tasks like authentication, logging, parsing requests, or managing errors inside applications. Example: app.use(express.json());
What is built-in middleware in Express.js?
Built-in middleware functions are provided directly by Express.js for handling common tasks like JSON parsing and serving static files. They simplify backend development without requiring external packages for basic functionalities. Example: app.use(express.static("public"));
What is custom middleware?
Custom middleware is a user-defined function created to handle specific application logic like authentication, logging, or validation. It executes before the final response and improves code reusability across multiple routes. Example: function logger(req, res, next) { next(); }
What is third-party middleware?
Third-party middleware refers to external npm packages integrated into Express.js applications to add additional functionality like authentication, security, logging, or request parsing without writing custom code manually. Example: const cors = require("cors"); app.use(cors());
What is the next() function in Express.js?
next() passes control from one middleware function to the next middleware or route handler. Without calling next(), the request-response cycle may stop, causing the client request to remain unfinished. Example: app.use((req, res, next) => { next(); });
What is route parameter in Express.js?
Route parameters are dynamic values included directly in the URL path. They help retrieve specific data like user IDs or product IDs from client requests inside Express.js routes. Example: app.get("/user/:id", (req, res) => {});
What is query parameter in Express.js?
Query parameters are key-value pairs added after the URL using ?. They are mainly used for filtering, searching, pagination, or sorting data in APIs and can be accessed using req.query. Example: app.get("/search?q=react");
What is Express Router?
Express Router is a mini routing system used to organize routes into separate files or modules. It improves code structure and maintainability, especially in large backend applications with multiple endpoints. Example: const router = express.Router();
How do you handle 404 errors in Express.js?
404 errors are handled by creating middleware for unmatched routes. If no route matches the request, Express sends a custom "Page Not Found" response to the client. Example: app.use((req, res) => { res.status(404).send("Not Found"); });
How do you handle global error handling in Express.js?
Global error handling uses special middleware with four parameters: err, req, res, and next. It catches application errors centrally and sends proper error responses without repeating error-handling logic in every route. Example: app.use((err, req, res, next) => { res.status(500).send(err.message); });
Request, Response & APIs
What is REST API in Express.js?
REST API is a communication standard used to perform operations like creating, reading, updating, and deleting data using HTTP methods. Express.js simplifies building RESTful APIs through routing and middleware support. Example: app.get("/users", (req, res) => {});
How do you send JSON responses in Express.js?
JSON responses are sent using the res.json() method. It automatically converts JavaScript objects into JSON format and sends them to the client with appropriate headers. Example: res.json({name: "John"});
What is body-parser in Express.js?
body-parser is middleware used to parse incoming request bodies like JSON or form data. Earlier versions required a separate package, but modern Express applications mostly use the built-in express.json() middleware instead. Example: app.use(express.json());
What is express.json() middleware?
express.json() is built-in middleware that parses incoming JSON request data and converts it into JavaScript objects accessible through req.body. It is commonly used in API development for handling client request data. Example: app.use(express.json());
How do you handle form data in Express.js?
Form data is handled using middleware like express.urlencoded() or express.json(). These middleware functions parse submitted form values and make them available inside req.body for processing. Example: app.use(express.urlencoded({ extended: true }));
What are HTTP status codes?
HTTP status codes are server responses indicating the result of client requests. Common codes include 200 for success, 404 for not found, and 500 for server errors. They help clients understand request outcomes clearly. Example: res.status(200).send("Success");
How do you create CRUD operations in Express.js?
CRUD operations are created using HTTP methods: GET for reading, POST for creating, PUT for updating, and DELETE for removing data. Express.js routing makes implementing CRUD APIs simple and organized. Example: app.post("/users", (req, res) => {});
How do you connect MongoDB with Express.js?
MongoDB connects with Express.js using libraries like Mongoose or the MongoDB driver. Developers establish database connections, define schemas, and perform database operations inside Express routes or controllers. Example: mongoose.connect("mongodb://localhost/test");
What is CORS in Express.js?
CORS, or Cross-Origin Resource Sharing, allows servers to accept requests from different domains. It prevents browser security restrictions when frontend and backend applications run on separate origins. Example: app.use(cors());
How do you secure an Express.js application?
Express.js applications are secured using authentication, input validation, HTTPS, helmet middleware, rate limiting, secure headers, and environment variables. Proper error handling and database sanitization also help prevent common security vulnerabilities. Example: app.use(helmet());