Express Js Interview Questions

Express Js Interview Questions




What is Express js?

Its a light-weight Widely used open source web framework which can use for both web application development and mobile app development using backend MongoDb with Node Js Platform. Express js is subpart of Nodejs.

Explain How to install  Express Js?

Step-1:  Install Node Js before Install Express js.
Step-2: Create Directory for your Project Folder.
$ mkdir myapp
$ cd myapp
Step-3:  Install the express js you can follow below command
$ npm install express –save
or
$ npm install express
here –save is used for temporally install so not added in dependency list.

What is role of CORS in Express JS?

Cross-Origin Resource Sharing (CORS). It gives permission to use agent to access from selected resources from a server on a different origin (domain). Add below lines of code in server.js to achieve it.

app.all('*', function(req, res, next) {
res.set('Access-Control-Allow-Origin', '*');
res.set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT');
res.set('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type');
if ('OPTIONS' == req.method) return res.send(200);
next();
});

How to Debug Express js App?

For Code debug you need to set the below line of command

// Linux Command
$ DEBUG=express:* node index.js
// Window Command
set DEBUG=express:* & node index.js

How to render plain Html in Express Js?

res.sendFile();
res.render();

Why Parameter “next” Used For In Express?

Next is the optional parameter in Express js, It passes control to the next matching route

app.get("/", function(httpRequest, httpResponse, next){
httpResponse.write("Hello");
next();
});

How To Control 404 Errors In Expressjs?

To control 404 error you have to simple two line of code in server.js in your app.

/* Define fallback route */
app.use(function(req, res, next) {
res.status(404).json({errorCode: 404, errorMsg: "route not found"});
});

What template engines supports with express?

Pug, Handlebars, Mustache, and EJS are the Top popular template engine which all are supports by Express JS. In Express application  default template engine is Pug.

How to receive Get and Post data  in ExpressJs?

app.get('/listing', function (req, res) {
//Receive get data
console.log(req.query);
});
app.post('/listing', function (req, res) {
//Receive post data
console.log(req.body);
});

How Express Js handle with Multi-threading

Node js won’t support multi threading, so to achieve this it deals with callback.

What do you mean by “Callback hell”?

If More callback call at time it will nested, So it will difficult to read, this is called Callback hell.

How to download a file in ExpressJs?

//res is the response which contain file name.

var file = __dirname + '/folder-name/download.js';
res.download(file); 

How to create a custom errorhandler middleware in ExpressJs

It easy to create a custom errorhandler if there is not installed any kind of inbuilt middleware.

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

How to handle Cookies in ExpressJs

You need to Install the “cookie-parser” module.
Use this command to npm install cookie-parser

app.get('/', function(req, res) {
console.log("Cookies: ", req.cookies)
})

How To get Pretty Html Output In Express.js?

Like other language Express js also has option to get better visualisation or readable

app.set('view options', { pretty: true });