Express Js Interview Questions
This page all about express js interview questions. Nowadays Apart from angular js Express Js also an important for web / mobile application development framework.
Table of Contents
- What is Express js?
- Explain How to install Express Js?
- What is the role of CORS in Express JS?
- How to Debug Express js App?
- How to render plain Html in Express Js?
- Why Parameter “next” Used For In Express?
- How To Control 404 Errors In Expressjs?
- What template does engines support with express?
- How to receive Get and Post data in ExpressJs?
- How Express Js handle with Multi-threading
- What do you mean by “Callback hell”?
- How to download a file in ExpressJs?
- How to create a custom error handler middleware in ExpressJs
- How to handle Cookies in ExpressJs
- How To Get Pretty Html Output In Express.js?
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 a 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 the dependency list.
What is the role of CORS in Express JS?
Cross-Origin Resource Sharing (CORS). It gives permission to use an agent to access selected resources from a server on a different origin (domain). Add below lines of code in server.js to achieve it.
1 2 3 4 5 6 7 |
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
1 2 3 4 |
// Linux Command $ DEBUG=express:* node index.js // Window Command set DEBUG=express:* & node index.js |
How to render plain Html in Express Js?
1 2 |
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
1 2 3 4 |
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 lines of code in server.js in your app.
1 2 3 4 |
/* Define fallback route */ app.use(function(req, res, next) { res.status(404).json({errorCode: 404, errorMsg: "route not found"}); }); |
What template does engines support 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?
1 2 3 4 5 6 7 8 |
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 multithreading, so to achieve this it deals with a 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 contains a file name.
1 2 |
var file = __dirname + '/folder-name/download.js'; res.download(file); |
How to create a custom error handler middleware in ExpressJs
It easy to create a custom error handler if there is not installed any kind of inbuilt middleware.
1 2 3 4 5 6 7 8 9 10 |
// 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
1 2 3 |
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 the option to get better visualization or readable
1 |
app.set('view options', { pretty: true }); |