Express JS Interview Questions and Answers

Q29 – What is express.js?
A – Express.js is a lightweight, flexible Node.js framework that is used for building server-side web applications.
Q30 – Give some features of express?
A – Following are the features of express:
- Express can be used to build single-page as well as multi-page applications. Moreover, it can also be used to build hybrid applications and APIs.
- Routing can be performed easily using express
- Express is single-threaded and asynchronous.
- Useful middlewares can be defined using express for handling HTTP or RESTful requests.
- Express can be used with both RDBMS and NoSQL databases.
Q31 – Differentiate between Node.js.js and express.js.
A – Following are the differences between Node.js and Express.js:
- Node.js is a JavaScript environment while Express.js is a Node.js framework.
- Node.js is built using Google Chrome’s V8 engine while Express.js is built on Node.js.
- Express.js provides features such as routing and middlewares but no such features are available in Node.js.
Q32 – What is a middleware function?
A – A function that has access to the request object, response object, and next function is called a middleware function.
Q33 – What is the use of the middleware function?
A – Middleware function is used to:
- Execute any kind of code
- to update the response or request objects.
- to call the next middleware
Q34 – What are the types of middlewares in express?
A – Types of middlewares in express are:
- Router-level middleware
- Built-in middleware
- Application-level middleware
- Error-handling middleware
- Third-party middleware
Q35 – What is a route handler?
A – A router handler in the express is a middleware function that executes when a matching request to its specified route is found.
Q36 – Name the parameters of a route handler.
A – The parameters of a route handler are:
- request object
- response object
- next() function
Q37 – What is the usage of the next() function?
A – The next() function is an optional parameter of a route handler. It is used to pass the execution to the next route.
Q38 – Observe the following route handler.
app.get(‘/user/:id’, function (req, res, next) {
}
How to access “id” in this route handler?
A – To access “id” from the router, we can use the “params” property of the request object. Observe the following code.
req.params.id
Q39 – What is the use of express.json()?
A – It is an express in-built middleware function that is used to parse the incoming requests with JSON data.
Q40 – What is CORS?
A – CORS stands for Cross-origin Resource Sharing. It is used to skip the same-origin policy so that AJAX requests can access the resources from any outside host.
Q41 – How to enable CORS in an express application?
A – To enable CORS, first install it using the following command.
npm install cors
And then, import the CORS to use it as a middleware.
app.use(cors())
Q42 – How to handle errors using middleware?
A – A middleware function in express can have an additional parameter, named “err”. The “err” holds the information regarding the error. This parameter should be the first parameter for the middleware.
Q43 – Give the ways for configuring properties in express?
A – There are two ways for configuring properties in the express.
- By using process.env
- By using Requirejs
Q44 – Explain the cookie.
A – Cookie is the information of a user’s action that is sent from the server side and stored on the client side.
Q45 – What is the purpose of using cookies?
A – The purpose of using cookies are:
- Tracking user
- Managing session
- Personalization
Q46 – Which package is needed to use cookies in express?
A – The cookie parser is needed to use cookies in the express.
Q47 – What is pug?
A – Pug is the most popular templating engine that is used with express.
Q48 – Explain scaffolding.
A – In simple terms, scaffolding is a tool that is used to create a structure of an express application.
Q49 – Name the types of Http requests.
A – Following are the types of Http requests.
- GET
- POST
- PATCH
- PUT
- DELETE
- HEAD
- CONNECT
- OPTION
- TRACE
Q50 – What is the difference between PUT and PATCH?
A – The PUT request modifies the entire resource while the PATCH request only modifies the specific part of the resource.