Express js Interview Questions And Answers

What is express.js?

Express.js is a web framework which is based on the core Node.js http module. It is a big strength of Node.js. It is an abstraction layer that stays on the top of the Node's built-in HTTP server.
What are the features of Express.js?

It is fast, unopinionated, minimalist web framework. It adds a number of helpful conveniences to Node.js's HTTP server. Express applications have middleware and routes, both complement one another.
Define request handler.

A request handler is a function that will be executed every time the server receives a particular request, usually defined by HTTP method. It accepts two parameters request and response.
How Express.js works?

Express.js has a main file, that is the main entry file. This file may contain configuration settings, template engine, routes, define middleware such as static file handler, error handling, cookies and other parsers.
What is the use of req.params?

The req.params is used to get the url path, query strings from within a specific request handler. This is basically an array with key value pairs.
What is the role of app.get() function?

The app.get() function in the first parameter accepts regular expression of the URL patterns in a string format and a request handler in the second parameter.
What is the use of view engine?

View Engine is the actual rendering of the page. There are many view engines are available for express.js, like - Handlebars, Mustache, Pug, and EJS. Among these engines the most popular view engine is the 'Pug'.
What is the app.use?

The method app.use() has one optional string parameter path and one mandatory function parameter callback. For example, to implement a logger with a date, time, request method and URL.
What is the role of process.env?

The process.env is the global object. All environment variables are get available through this object. Suppose, we have to set the PORT in variable, then this will be accessible through process.env.PORT.
When should we set the trust proxy to true?

If your app is working behind reverse proxy such as Nginx, Varnish, set the trust proxy to true.

How error is handled in Express.js?

Error Handling in the Express is very simple task. This is done by using a type of middleware called error handling middleware.
function(err, req, res, next) {

Here, the first argument is the error. When your application is in error mode, all regular middleware is ignored. If no error happens, then the error handling middleware will never execute. And if the error occurs, then express skips over other middleware and takes the error handling middleware in the stack.
What is Middleware?

In express.js, we can call several request handler functions that can deal with small pieces of code rather than one request handler. These smaller request handler functions are called Middleware.
How to handle cookie in Express.js?

The express.cookieParser() allows us to access user cookie values from the req.cookie object in request handlers. The method takes a string which is used for signing cookies.
What are the options of express.session()?

The express.session() takes the following options -
  • key: cookie name defaulting to connect.sid
  • store: session store instance, usually Redis object (more about it in the Redis chapter)
  • secret: session cookie is signed with this secret to prevent tampering, usually just a random string
  • cookie: session cookie settings, defaulting to { path: '/', httpOnly: true, maxAge: null }
  • proxy: boolean that says to trust the reverse proxy when setting secure cookies (via "xforwarded- proto")
What are the available options for express.static()?

The express.static method takes these options -
  • maxAge: number of milliseconds to set for browser cache maxAge, which defaults to 0.
  • hidden: boolean true or false which enables serving of hidden files; defaults to false for security reasons.
  • redirect: boolean true or false (default is true) that allows for a redirect to a trailing slash "/" when the URL pathname is a directory.

Write basic Express.js authentication program?

The very basic HTTP authentication program is as follows -
app.use(express.basicAuth(function(user, pass){
if (user === 'sect' && pass === '32324' ) {
	return true;
} else {
	return false;
How can we handle file upload in express.js?

req.files is used to handle file uploads.
How to serve the static files in Express.js?

To serve the static files, use the express.static built-in middleware function.
express.static(root, [options])

The first parameter is the root directory which to serve static assets. Suppose, we want to serve files of a directory name 'public' -
How can we see the all internal logs used in Express?

To see all the internal logs used in Express.js, we need to set the DEBUG Environment to Express.*
DEBUG = express:* node index.js
How can we see the all internal logs used in Express?

To see all the internal logs used in Express.js, we need to set the DEBUG Environment to Express.*
DEBUG = express:* node index.js

What is Scaffolding?

Scaffolding is used to create a skeleton structure for our application. This is supported by many MVC frameworks. This reduces lots of development cost, as it has predefined templates that we can use for CRUD operations.