ExpressJs Interview Questions and Answers

Q1. What is Express.js?
Express.js is a light-weight node.js based web application framework. This JavaScript framework provides a number of flexible and useful feature in order to develop mobile as well as web application using NodeJS.

Q2. How to install express js in node?
use following command to install express js.
npm install express

Q3. What type of web application can built using Express JS?
you can build single-page, multi-page, and hybrid web applications.

Q4. Why to use Express?
You should use Express because provides following features:
1.Parses the arguments and headers
2.Supports Routing
3.Supports multiple view engines like Jade, EJS, JSHtml etc.
4.Handle Configurations
5.Supports Sessions
6.Supports Content Negotiation
7.Supports Error Handling
8.Supports Multiple Databases
o RDBMS – MySQL, MS SQL etc.
o NoSQL – MongoDB, Firebase, Redis etc.

Q5 List out the ExpressJS Features
Following are some of the core features of Express framework −

  • Set up middlewares in order to respond to HTTP/RESTful Requests.
  • It is possible to defines a routing table in order to perform different HTTP operations.
  • Dynamically renders HTML Pages based on passing arguments to templates.
  • Provides all the feature provides by core Node.js.
  • Express prepare a thin layer, therefore, the performance is adequate.
  • Organize the web application into an MVC architecture.
  • Manages everything from routes to rendering view and preforming HTTP request.

Q6. How to create an Http server using Express?
Express is best for developing web application using Node.js. You can create an http server using express as given below:
var express = require(“express” );
var app = express();
app.get( “/”, function (req, res) {
res.write(“Hello, Express”);
var port = process.env.port || 1305;
console.log(“Server is running at http://localhost:” + port);

Q7. How To Install Expressjs?
Assuming you’ve already installed Node.js, create a directory to hold your application, and make that your working directory.
$ mkdir myapp
$ cd myapp
Use the npm init command to create a package.json file for your application. For more information on how package.json works, see Specifics of npm’s package.json handling.

$ npm init
This command prompts you for a number of things, such as the name and version of your application. For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:

entry point: (index.js)
Enter app.js, or whatever you want the name of the main file to be. If you want it to be index.js, hit RETURN to accept the suggested default file name.

Now install Express in the myapp directory and save it in the dependencies list. For example:
$ npm install express –save
To install Express temporarily and not add it to the dependencies list, omit the –save option:
$ npm install express

Q8. What are different methods in REST API?
GET : Used to read.
POST: Used to update.
PUT: Used to create.
DELETE: Used to delete

Q9. How to use express js in node?
use require to include express module.
var app = require(‘express’)();

Q10. How to use handle get request in express Js?
/*Include require module*/
var app = require(‘express’)();
var http = require(‘http’).Server(app);
app.get(‘/’, function (req, res) {
console.log(“Got a GET request for the homepage”);
//Shown in console res.send(‘This is GET Method for Homepage’); //
Display as response
/*Start listing 8080 port*/
http.listen(‘8080’, function() {
console.log(‘listening on *:8080’);

Q11. What Function Arguments Are Available To Express.js Route Handlers?
The arguments available to an Express.js route handler function are:

  • req – the request object
  • res – the response object
  • next (optional) – a function to pass control to one of the subsequent route handlers

The third argument may be omitted, but is useful in cases where you have a chain of handlers and you would like to pass control to one of the subsequent route handlers, and skip the current one.

Q12.What is template engine?
A template engine allows us to create and render an HTML template with minimal code. At runtime, a template engine executes expressions and replaces variables with their actual values in a template file. In this way, it transforms the template into an HTML file and sent to it the client.

Q13. What template engines you can use with express?
The popular template engines which you can use with Express are Pug, Handlebars, Mustache, and EJS. The Express application generator uses Pug as its default template engine.

Q14. How to download a file?
app.get(‘/download’, function(req, res){
var file = __dirname + ‘/download-folder/file.txt’;;

Q15. How to get variables in Express.js in GET Method? 
var express = require(‘express’);
var app = express();
app.get(‘/’, function(req, res){
/* req have all the values **/
res.send(‘id: ‘ +;

Q16. How to get POST a query in Express.js?
var bodyParser = require(‘body-parser’) app.use( bodyParser.json() );
// to support JSON-encoded
// to support URL-encoded
extended: true

Q17. What is the parameter “next” used for in Express? 
app.get(‘/userdetails/:id?’, function(req, res, next){ });
req and res which represent the request and response objects
nextIt passes control to the next matching route.

Q18. What is body-parser
Parse incoming request bodies in a middleware before your handlers, available under the req.body property.
To handle HTTP POST request in Express.js version 4 and above, you need to install middleware module called body-parser.
body-parser extract the entire body portion of an incoming request stream and exposes it on req.body.
$ npm install body-parser
var bodyParser = require(‘body-parser’)

var express = require(‘express’)
var bodyParser = require(‘body-parser’)
var app = express()
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(function (req, res) {
res.setHeader(‘Content-Type’, ‘text/plain’)
res.write(‘you posted:\n’)
res.end(JSON.stringify(req.body, null, 2))

Review Date
Reviewed Item
Good job. Thanks for updating
Author Rating