Google News
logo
Express.js Interview Questions
See the Example :
app.use(express.static('public'))  
app.use('/static', express.static(path.join(__dirname, 'public')))  
The following HTTP methods are the most used ones :
 
GET : A specified resource's representation is requested by the GET method. These requests can only retrieve data.

POST : Posting of the data enclosed in the request as a new entity is done using the POST method. The entity is identified by the URI.

PUT : Modification in the existing entity is done with the data enclosed in the request identified by the URI.

DELETE : The request for deleting the specified source is made by the DELETE method.
A cross-Origin can fail in the following scenarios-
 
* If it's to a different domain
* If it's to a different port
* If it's to a different subdomain
* If it's to a different protocol.
We will first install it using the given command:
npm install pug
Then we will set it as following when initializing the Express app :
const express = require ('express')
const app = express()
app.set ('view engine', 'pug')
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"});
});
The application-level middleware method is used to bind to the app object using app.use() method. It applies on all routes.
//This middleware will execute for each route.  
app.use(function (req, res, next) {  
  console.log('Current Time:', Date.now())  
  next()  
})  
The router-level Middleware is used to bind to a specific instance of express.Router().Built-in Middleware : The built-in Middleware was introduced with version 4.x. It ends the dependency on Connect.
 
There are the following built-in middleware functions in Express.js :
 
* static : It is used to serve static assets such as HTML files, images, etc.

* json : It is used to parse the incoming requests with JSON payloads. It is available with Express 4.16.0+

* urlencoded : It is used to parse the incoming requests with URL-encoded payloads. It is available with Express 4.16.0+
We can call the following methods in such cases -
 
File.name : the name of the file

File.path : the path to which the file is written

File.size : the size of the file in bytes

File.type : the file's MIME-type.
Cluster :
 
* Each CPU has one process with an IPC to communicate.
* Clusters help when multiple servers are required to accept HTTP requests through a single port.
* The processes have separate memory because of being spanned in different CPUs leading to memory issues.

Worker Threads :
 
* Only a single process is there with multiple threads.
* Each Node contains one Node having most APIs accessible.
* The memory is shared with other threads.
* We can use this for CPU-intensive tasks.