Node js express js
Express JS is an application framework that is light-weighted node JS. Variety of versatile, helpful and vital options are provided by this JavaScript framework for the event of mobile additionally as internet applications with the assistance of node JS.
Express JS Use – Express.js could be a light-weight internet application that helps in organizing the net application into MVC design on the server aspect.
Following are the steps accustomed for An Express JS application:
A folder with a constant name because the project name is made.
A file named package.json is made within the folder created.
“npm install” command is run on the electronic communication. It installs all the libraries gift in package.json.
A file named server.js is made.
“Router” file is made within the package that consists of a folder named index.js.
“App” is made within the package that has the index.html file.
Let us move on to the next Express JS Interview Questions
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 that is employed to pass management to 1 of the following route handlers.
The third argument is optional and should be omitted, however, in some cases, it’s helpful wherever there’s a series of handlers and management will be passed to 1 of the following route handlers skipping this one.
In Express JS, there are 2 ways that for configuring the properties:
With process.ENV:
A file with the name “.env” is to be created within the project folder.
All the properties are to be other within the “.env” file.
Any of the properties will be employed in server.js.
With require.JS:
A file with the name “config.json” is to be created within the config folder within the project folder.
The config properties are to be present within the config.json file.
Now, ought to be accustomed to access the config.json file.
This is the basic Express JS Interview Questions asked in an interview. There is no definitive answer to the current question. The solution depends on the dimensions of your application and therefore the team that’s concerned.
Routes and alternative application-Express logic will board as several files as you would like, in any directory structure you favor. Read the subsequent examples for inspiration:
Route listings
Route map
MVC vogue controllers
Also, there are third-party extensions for Express JS applications that modify a number of these patterns:
Resourceful routing
In order to permit CORS in Express.js, add the subsequent code in server.js:
For Example:
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();
});
In different operative Systems, we’ve got following commands:
On UNIX operating system the command would be as follows:
$ DEBUG=express:* node index.js
On Windows the command would be:
set DEBUG=express:* & node index.js
From Webstrome IDE
C:\Program Files (x86)\JetBrains\WebStorm 2016.2.4\bin\runnerw.exe" "C:\Program Files\nodejs\node.exe" --debug-brk=61081 --expose_debug_as=v8debug E:\Development\nodejd\librarey\bin\www
Let us move on to the next advanced Express JS Interview Questions.
From Express 4.0 Error handling is easier. The steps are as following:
Create a middleware as following:
// error handler
app.use(function(err, req, res, next) solely 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');
});
Install Error Handler Middleware:
Install errorhandler
npm install errorhandler --save
Create a variable
var e errorhandler = require('errorhandler')
Use the middleware as following:
if (process.env.NODE_ENV === 'development') solely use in development
app.use(errorhandler())
}
function errorNotification(err, str, req) power unit title = 'Error in ' + req.method + ' ' + req.url
notifier.notify()
}
Next -It passes management to a consecutive matching route. OR a operate to pass management to 1 of the following route handlers.
The argument could also be omitted, however, is beneficial in cases wherever you have got a series of handlers and you’d wish to pass management to 1 of the following route handlers, and skip this one.
app.get('/user details/:id?', function(req, res, next));
Req and Res: It represents the request and response objects
Next: It passes management to a consecutive matching route.
In server.js add the subsequent code to send 404 errors back to a page in our ExpressJS App:
/* Define fallback route */
app.use(function(req, res, next) {
res.status(404).json({errorCode: 404, errorMsg: "route not found"});
});