Loading

Quipoin Menu

Learn • Practice • Grow

express-js / express-js - interview
interview

Q1. What is routing in Express.js?
Routing refers to how an application responds to client requests at specific endpoints (URIs) with specific HTTP methods (GET, POST, etc.). Each route has a route path, an HTTP method, and one or more handler functions. Example: app.get('/users', (req, res) => { ... }).

Q2. How do you define a basic route in Express?
Use app.METHOD(PATH, HANDLER). METHOD is the HTTP method in lowercase (get, post, put, delete). PATH is the route path (e.g., '/home'). HANDLER is the callback function that receives request and response objects. Example: app.get('/about', (req, res) => { res.send('About Page') }).

Q3. What is the difference between app.use() and app.get()?
app.use() mounts middleware for all HTTP methods at a specified path (or all paths if not specified). app.get() only handles GET requests. app.use() is typically used for middleware, static files, or routers, while app.get() is for specific GET route handlers.

Q4. Can you chain multiple handlers for a route?
Yes, you can provide multiple callback functions as arguments. They behave like middleware and must call next() to pass control to the next handler. Example: app.get('/example', (req, res, next) => { console.log('First'); next(); }, (req, res) => { res.send('Second'); });

Q5. What happens if no route matches a request?
Express will respond with a 404 Not Found error automatically. You can also create a custom 404 handler by adding middleware at the end of all routes: app.use((req, res) => { res.status(404).send('Page not found') });