Express JS interview questions
Express.js is a free and open source software and web application framework for Node JS which was designed for the development of web applications and API’s. It is also the standard server framework for Node JS. It was developed by TJ Holowaychuk on 16th November 2010. Along with the MongoDB database and Angular JS frontend framework, express JS is the backend part of the MEAN stack. With the help of this, you will be able to manage everything from routes to handling requests and views. It even provides a robust set of features for various web and mobile applications.
Interview questions for Express JS: –
Q1. List some features of Express JS.
Some of the main features of Express JS are listed below: –
- It is used for setting up middlewares so as to provide a response to the HTTP or RESTful requests.
- With the help of express JS, the routing table can be defined for performing various HTTP operations.
- It is also used for dynamically rendering HTML pages which are based on passing arguments to the templates.
- It provides each and every feature which is provided by core Node JS.
- The performance of Express JS is adequate due to the presence of a thin layer prepared by the Express JS.
- It is used for organizing the web applications into the MVC architecture.
- Everything from routes to rendering view and performing HTTP requests can be managed by Express JS.
Q2. Write the steps for setting up an Express JS application.
Following are the steps used to set up an express JS application: –
- A folder with the same name as the project name is created.
- A file named package.json is created inside the folder created.
- “npm install” command is run on the command prompt. It installs all the libraries present in package.json.
- A file named server.js is created.
- “Router” file is created inside the package which consists of a folder named index.js.
- “App” is created inside the package which has the index.html file.
This way, an express JS application is set up.
Q3. What do you mean by Express JS?
Q4. Name the type of web applications which can be built using Express JS.
Single-page, multi-page, and hybrid web applications can be built using Express JS.
Q5. What is the use of Express JS?
Express.js is a lightweight web application which helps in organizing the web application into MVC architecture on the server side.
Q6. What function are arguments available to Express JS route handlers?
The arguments which are available to an Express JS route handler-function are-
- Req – the request object
- Res – the response object
- Next (optional) – a function which is used to pass control to one of the subsequent route handlers.
The third argument is optional and may be omitted, but in some cases it is useful where there is a chain of handlers and control can be passed to one of the subsequent route handlers skipping the current one.
Q7. How to config properties in Express JS?
In Express JS, there are two ways for configuring the properties:
- With process.ENV:
- A file with the name “.env” is to be created inside the project folder.
- All the properties are to be added in the “.env” file.
- Any of the properties can be used in server.js.
- With require JS:
- A file with the name “config.json” is to be created in the config folder inside the project folder.
- The config properties are to be added in the config.json file.
- Now, require should be used to access the config.json file.
Q8. How can models be defined in Express JS?
There is no notion of any database in Express JS. So, the concept of models is left up to third-party node modules, allowing the users to interface with nearly any type of database.
Q9. How to authenticate users in express JS?
Since authentication is an opinionated area which is not ventured by express JS, therefore any authentication scheme can be used in express JS for the authentication of users.
Q10. Which template engine is supported by express JS?
Express JS supports any template engine that conforms to the (path, locals, callback) signature.
Q11. How can plain HTML be rendered in express JS?
There’s no need to render HTML with the res.render () function. If there’s a specific file, then you should use the res.sendFile () function. If any assets are being served from a dictionary, then express.static () middleware function needs to be used.