ExpressJS Interview Questions

What is Express.js? What are core features of Express framework?

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.

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.

What is Express?
Express is a web development framework for Node.js. Express is used to build single (SPA), multipage(MPA) and hybrid web applications and REST API using Node.js.

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

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");
res.end();
});
var port = process.env.port || 1305;
app.listen(port);
console.log("Server is running at http://localhost:" + port);


How to define routes using Express?
Express provides an efficient way to manage and handle routes in a web application. Express provides methods like get(), post(), put(), delete(), and so on to deal with HTTP verbs – get, post, put and delete respectively.
All of these methods take the first parameter as representing URL and a sequence of middleware as arguments.
Let’s see the examples to create routes in express.

var express = require("express" );
var app = express();
//route for home page
app.get( "/"function (req, res) {
res.write("This is Home Page" );
res.end();
});
//route for about us page
app.get( "/about"function (req, res) {
res.send( "<h1>This is About Us Page</h1>" );
});
var port = process.env.port || 1305;
app.listen(port);
console.log("Server is running at http://localhost:" + port);



What is the use of Express Router Class?
Express router class was introduced with Express 4.0. This provides routing APIs like. use, .get, .param, and route to deal with routes in a web application. Following is an example to define routes using router class.

var express = require("express" );
// get an instance of router
var router = express.Router();
var app = express();
// home page route
router.get( "/"function (req, res) {
res.send( "This is a Home Page" );
});
// about page route
router.get( "/about"function (req, res) {
res.send( "This is a About Us Page" );
});
// apply the routes to our application
app.use( '/' , router);
//running server
var port = process.env.port || 1306;
app.listen(port);
console.log("Server is running at http://localhost:" + port);


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.

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.

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.

Installation
$ npm install body-parser

API
var bodyParser = require('body-parser')

Example

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(bodyParser.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))
})


More Usefull Links: 

3 comments:

Topics

ADO .Net (2) Ajax (1) Angular Js (17) Angular2 (24) ASP .Net (14) Azure (1) Breeze.js (1) C# (49) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) Dotnet (21) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (9) Lamda (3) Linq (11) Mongodb (1) MVC (48) NodeJS (7) RDLC (1) Report (1) Sql Server (29) SSIS (3) SSRS (2) UI (1) WCF (12) Web Api (10) Web Service (1) XMl (1)