Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
23 views3 pages

ExpressJS Interview Questions

Uploaded by

arikaleeswaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views3 pages

ExpressJS Interview Questions

Uploaded by

arikaleeswaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Express.

js Interview Questions & Answers (For


Freshers)

What is Express.js?
Express.js is a minimal and flexible Node.js web application framework that provides a robust set of
features to develop web and mobile applications. It simplifies building server-side applications by
offering built-in support for routing, middleware, template engines, and handling HTTP requests.

Why use Express.js instead of just Node.js?


While Node.js provides a runtime environment and modules to work with files, networking, and
HTTP, it does not provide a structured way to build applications. Express.js builds on top of Node.js
and provides features like routing, middleware support, request/response handling, and better
scalability, making development faster and easier.

How do you install Express.js?


Express.js can be installed using npm (Node Package Manager). Command: npm install express
This installs the Express library into your project so you can import and use it.

What is middleware in Express.js?


Middleware are functions that sit between the request and the response cycle of an application.
They can execute code, modify the request or response objects, end the request-response cycle, or
call the next middleware in the stack. Examples include authentication, logging, and error handling.

What is the difference between app.use() and app.get()?


app.use() is used to apply middleware functions for all routes and HTTP methods. For example,
using app.use(express.json()) applies JSON body parsing to all requests. app.get(), on the other
hand, defines a route handler for HTTP GET requests at a particular endpoint.

What is routing in Express.js?


Routing refers to defining endpoints (URIs) in your application that respond to client requests. Each
route is associated with an HTTP method (GET, POST, PUT, DELETE) and a handler function that
defines what happens when a request matches that route.

How do you define routes in Express?


You can define routes using methods like app.get(), app.post(), app.put(), etc. Example:
app.get('/home', (req, res) => { res.send('Welcome Home!'); });

Difference between res.send() and res.json()?


res.send() can send a response of various types like string, object, or buffer. res.json() specifically
sends a JSON response and ensures proper content-type headers are set.

How do you handle route parameters in Express.js?


You can capture values from the URL using route parameters. For example: app.get('/user/:id',
(req, res) => { res.send(`User ID: ${req.params.id}`); });

What is next() in Express middleware?


The next() function is used to pass control from one middleware to the next. Without calling next(),
the request-response cycle will be left hanging unless the middleware ends the response.

What are built-in middlewares in Express.js?


Some commonly used built-in middlewares are: - express.json(): Parses incoming JSON requests. -
express.urlencoded(): Parses URL-encoded data (like HTML form submissions). - express.static():
Serves static files such as images, CSS, and JavaScript.

How to create custom middleware in Express?


You can create middleware functions to perform tasks such as logging or authentication. Example:
app.use((req, res, next) => { console.log(`${req.method} ${req.url}`); next(); });

How to handle errors in Express.js?


Error-handling middleware has four parameters: (err, req, res, next). Example: app.use((err, req,
res, next) => { res.status(500).send({ error: err.message }); });

What is the difference between Express Router and app instance?


express.Router() is used to create modular route handlers. It acts like a mini Express app. The app
instance (created using express()) is the main application object.

How do you serve static files in Express.js?


You can serve static files (images, CSS, JavaScript) using express.static(). Example:
app.use(express.static('public'));

What is the difference between synchronous and asynchronous


middleware?
Synchronous middleware runs line by line and blocks until complete. Asynchronous middleware
uses promises or callbacks and allows other tasks to continue while waiting for an operation (like
database queries or API calls) to finish.

How do you connect a database with Express.js?


You can connect to databases using drivers or Object Relational Mappers (ORMs). Example: -
MongoDB: Use Mongoose. - MySQL/PostgreSQL: Use Sequelize or Prisma. The database
connection is usually established in a separate module and imported into Express routes.

What is CORS in Express.js and how do you enable it?


CORS (Cross-Origin Resource Sharing) allows restricted resources on a web page to be requested
from another domain. In Express, you can enable it by installing and using the 'cors' package: const
cors = require('cors'); app.use(cors());

You might also like