ExpressJS Answers
0:000:00
Express.js Answers
Basic Express.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is Express.js? | A minimal and flexible Node.js web application framework | Provides features for building web and mobile applications |
2 | What is the purpose of Express.js? | Building web servers, APIs, and web applications using Node.js | Handling HTTP requests and responses, routing, middleware |
3 | How do you install Express.js? | Using npm install express | npm install express |
4 | How do you create a basic Express app? | Import Express, create an app instance, and start the server | const express = require('express'); const app = express(); app.listen(3000, ...); |
5 | What is a route? | A specific endpoint (URL path and HTTP method) that the application responds to | app.get('/', (req, res) => { ... }); |
6 | How do you define a GET route? | Using app.get() | app.get('/users', (req, res) => { res.send('Users'); }); |
7 | How do you define a POST route? | Using app.post() | app.post('/users', (req, res) => { ... }); |
8 | What is the req object? | Represents the incoming HTTP request | req.params , req.query , req.body , req.headers |
9 | What is the res object? | Represents the HTTP response sent back to the client | res.send() , res.json() , res.status() , res.render() |
10 | How do you send a text response? | Using res.send() | res.send('Hello World!'); |
11 | How do you send a JSON response? | Using res.json() | res.json({ message: 'Success' }); |
12 | How do you set the HTTP status code? | Using res.status() | res.status(404).send('Not Found'); |
13 | What is middleware? | Functions that have access to req , res , and the next function | app.use((req, res, next) => { ... }); |
14 | How do you use middleware? | Using app.use() | app.use(express.json()); |
15 | What is express.json() middleware? | Built-in middleware to parse incoming requests with JSON payloads | app.use(express.json()); |
Intermediate Express.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What are route parameters? | Placeholders in a route path that capture values | app.get('/users/:id', (req, res) => { res.send(req.params.id); }); |
2 | What are query parameters? | Key-value pairs appended to the URL after a ? | app.get('/search', (req, res) => { res.send(req.query.q); }); |
3 | What is express.Router() ? | A way to create modular, mountable route handlers | const router = express.Router(); router.get('/', ...); app.use('/users', router); |
4 | What is error handling middleware? | Middleware functions with four arguments ((err, req, res, next) ) to catch errors | app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!'); }); |
5 | What is express.static() middleware? | Built-in middleware to serve static assets (CSS, JS, images) from a directory | app.use(express.static('public')); |
6 | What is a template engine in Express? | Allows you to render dynamic HTML pages (e.g., EJS, Pug, Handlebars) | app.set('view engine', 'ejs'); res.render('index', { name: 'User' }); |
7 | How do you configure a template engine? | Using app.set('view engine', 'engine-name') | app.set('view engine', 'ejs'); |
8 | What is CORS (Cross-Origin Resource Sharing)? | A mechanism that allows a web page to request resources from a different domain (origin) | Using cors middleware: app.use(cors()); |
9 | What is the cors middleware? | A third-party middleware to enable CORS | const cors = require('cors'); app.use(cors()); |
10 | How do you handle form submissions (POST data)? | Using express.urlencoded() middleware | app.use(express.urlencoded({ extended: true })); |
11 | What is express.urlencoded() middleware? | Parses incoming requests with URL-encoded payloads (like application/x-www-form-urlencoded ) | app.use(express.urlencoded({ extended: true })); |
12 | What is the difference between app.use() and app.METHOD() ? | app.use() applies middleware globally or to specific paths; app.METHOD() defines a specific route | app.use() vs app.get() |
13 | How do you chain middleware? | Apply multiple middleware functions sequentially | app.get('/', middleware1, middleware2, routeHandler); |
14 | What is the purpose of next() in middleware? | Passes control to the next middleware function in the stack | app.use((req, res, next) => { ... next(); }); |
15 | How do you handle environment variables? | Using process.env and often libraries like dotenv | require('dotenv').config(); const port = process.env.PORT || 3000; |
Advanced Express.js Answers
# | Question | Answer | Examples |
---|---|---|---|
1 | What is error handling middleware in Express.js? | A special middleware function with four arguments ((err, req, res, next) ) to catch errors | app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!'); }); |
2 | What is rate limiting? | Restricting the number of requests a client can make to an API within a specific time frame | Using middleware libraries like express-rate-limit to prevent abuse. |
3 | What is input validation? | Ensuring incoming data meets specific format and type requirements | Using libraries like Joi, Zod, express-validator |
4 | How do you handle authentication and authorization? | Verifying a user's identity (authentication) and determining what they are allowed to do (authorization) | Using JWT (JSON Web Tokens), OAuth, session-based authentication. |
5 | What is the concept of caching in Node.js? | Storing frequently accessed data in a faster store (e.g., Redis, Memcached) to improve performance | Using libraries like node-cache or integrating with Redis. |
6 | What is the purpose of WebSockets? | Enables real-time, bidirectional communication between client and server over a single connection | Real-time chat applications, live updates (e.g., using Socket.IO). |
7 | What is the concept of GraphQL? | A query language for APIs that allows clients to request exactly the data they need | Using Apollo Server, Express-GraphQL |
8 | What is a message queue? | A system for asynchronous communication between application components (e.g., RabbitMQ, Kafka) | Using libraries like bull , amqplib |
9 | How do you handle testing in Node.js? | Using frameworks like Jest, Mocha, Chai, Supertest | Unit tests, integration tests, end-to-end tests. |
10 | What is the purpose of helmet middleware? | Secures Express apps by setting various HTTP headers | app.use(helmet()); |
11 | What is the purpose of compression middleware? | Compresses responses using Gzip or Brotli algorithms to reduce transfer size | app.use(compression()); |
12 | What is the purpose of morgan middleware? | HTTP request logger middleware | app.use(morgan('dev')); |
13 | How do you manage sessions in Express.js? | Using middleware like express-session to store user session data on the server | app.use(session({ secret: 'keyboard cat', resave: false, saveUninitialized: false })); |
14 | What is the difference between RESTful APIs and GraphQL? | REST uses resources and HTTP methods for CRUD; GraphQL allows clients to specify data needs precisely | GraphQL can be more efficient for complex data requirements, avoiding over-fetching/under-fetching. |
15 | How do you handle asynchronous middleware in Express? | Using async/await within middleware functions | app.use(async (req, res, next) => { await someAsyncTask(); next(); }); |