
Domains
Agile Management
Master Agile methodologies for efficient and timely project delivery.
View All Agile Management Coursesicon-refresh-cwCertifications
Scrum Alliance
16 Hours
Best Seller
Certified ScrumMaster (CSM) CertificationScrum Alliance
16 Hours
Best Seller
Certified Scrum Product Owner (CSPO) CertificationScaled Agile
16 Hours
Trending
Leading SAFe 6.0 CertificationScrum.org
16 Hours
Professional Scrum Master (PSM) CertificationScaled Agile
16 Hours
SAFe 6.0 Scrum Master (SSM) CertificationAdvanced Certifications
Scaled Agile, Inc.
32 Hours
Recommended
Implementing SAFe 6.0 (SPC) CertificationScaled Agile, Inc.
24 Hours
SAFe 6.0 Release Train Engineer (RTE) CertificationScaled Agile, Inc.
16 Hours
Trending
SAFe® 6.0 Product Owner/Product Manager (POPM)IC Agile
24 Hours
ICP Agile Certified Coaching (ICP-ACC)Scrum.org
16 Hours
Professional Scrum Product Owner I (PSPO I) TrainingMasters
32 Hours
Trending
Agile Management Master's Program32 Hours
Agile Excellence Master's ProgramOn-Demand Courses
Agile and ScrumRoles
Scrum MasterTech Courses and Bootcamps
Full Stack Developer BootcampAccreditation Bodies
Scrum AllianceTop Resources
Scrum TutorialProject Management
Gain expert skills to lead projects to success and timely completion.
View All Project Management Coursesicon-standCertifications
PMI
36 Hours
Best Seller
Project Management Professional (PMP) CertificationAxelos
32 Hours
PRINCE2 Foundation & Practitioner CertificationAxelos
16 Hours
PRINCE2 Foundation CertificationAxelos
16 Hours
PRINCE2 Practitioner CertificationSkills
Change ManagementMasters
Job Oriented
45 Hours
Trending
Project Management Master's ProgramUniversity Programs
45 Hours
Trending
Project Management Master's ProgramOn-Demand Courses
PRINCE2 Practitioner CourseRoles
Project ManagerAccreditation Bodies
PMITop Resources
Theories of MotivationCloud Computing
Learn to harness the cloud to deliver computing resources efficiently.
View All Cloud Computing Coursesicon-cloud-snowingCertifications
AWS
32 Hours
Best Seller
AWS Certified Solutions Architect - AssociateAWS
32 Hours
AWS Cloud Practitioner CertificationAWS
24 Hours
AWS DevOps CertificationMicrosoft
16 Hours
Azure Fundamentals CertificationMicrosoft
24 Hours
Best Seller
Azure Administrator CertificationMicrosoft
45 Hours
Recommended
Azure Data Engineer CertificationMicrosoft
32 Hours
Azure Solution Architect CertificationMicrosoft
40 Hours
Azure DevOps CertificationAWS
24 Hours
Systems Operations on AWS Certification TrainingAWS
24 Hours
Developing on AWSMasters
Job Oriented
48 Hours
New
AWS Cloud Architect Masters ProgramBootcamps
Career Kickstarter
100 Hours
Trending
Cloud Engineer BootcampRoles
Cloud EngineerOn-Demand Courses
AWS Certified Developer Associate - Complete GuideAuthorized Partners of
AWSTop Resources
Scrum TutorialIT Service Management
Understand how to plan, design, and optimize IT services efficiently.
View All DevOps Coursesicon-git-commitCertifications
Axelos
16 Hours
Best Seller
ITIL 4 Foundation CertificationAxelos
16 Hours
ITIL Practitioner CertificationPeopleCert
16 Hours
ISO 14001 Foundation CertificationPeopleCert
16 Hours
ISO 20000 CertificationPeopleCert
24 Hours
ISO 27000 Foundation CertificationAxelos
24 Hours
ITIL 4 Specialist: Create, Deliver and Support TrainingAxelos
24 Hours
ITIL 4 Specialist: Drive Stakeholder Value TrainingAxelos
16 Hours
ITIL 4 Strategist Direct, Plan and Improve TrainingOn-Demand Courses
ITIL 4 Specialist: Create, Deliver and Support ExamTop Resources
ITIL Practice TestData Science
Unlock valuable insights from data with advanced analytics.
View All Data Science Coursesicon-dataBootcamps
Job Oriented
6 Months
Trending
Data Science BootcampJob Oriented
289 Hours
Data Engineer BootcampJob Oriented
6 Months
Data Analyst BootcampJob Oriented
288 Hours
New
AI Engineer BootcampSkills
Data Science with PythonRoles
Data ScientistOn-Demand Courses
Data Analysis Using ExcelTop Resources
Machine Learning TutorialDevOps
Automate and streamline the delivery of products and services.
View All DevOps Coursesicon-terminal-squareCertifications
DevOps Institute
16 Hours
Best Seller
DevOps Foundation CertificationCNCF
32 Hours
New
Certified Kubernetes AdministratorDevops Institute
16 Hours
Devops LeaderSkills
KubernetesRoles
DevOps EngineerOn-Demand Courses
CI/CD with Jenkins XGlobal Accreditations
DevOps InstituteTop Resources
Top DevOps ProjectsBI And Visualization
Understand how to transform data into actionable, measurable insights.
View All BI And Visualization Coursesicon-microscopeBI and Visualization Tools
Certification
24 Hours
Recommended
Tableau CertificationCertification
24 Hours
Data Visualization with Tableau CertificationMicrosoft
24 Hours
Best Seller
Microsoft Power BI CertificationTIBCO
36 Hours
TIBCO Spotfire TrainingCertification
30 Hours
Data Visualization with QlikView CertificationCertification
16 Hours
Sisense BI CertificationOn-Demand Courses
Data Visualization Using Tableau TrainingTop Resources
Python Data Viz LibsCyber Security
Understand how to protect data and systems from threats or disasters.
View All Cyber Security Coursesicon-refresh-cwCertifications
CompTIA
40 Hours
Best Seller
CompTIA Security+EC-Council
40 Hours
Certified Ethical Hacker (CEH v12) CertificationISACA
22 Hours
Certified Information Systems Auditor (CISA) CertificationISACA
40 Hours
Certified Information Security Manager (CISM) Certification(ISC)²
40 Hours
Certified Information Systems Security Professional (CISSP)(ISC)²
40 Hours
Certified Cloud Security Professional (CCSP) Certification16 Hours
Certified Information Privacy Professional - Europe (CIPP-E) CertificationISACA
16 Hours
COBIT5 Foundation16 Hours
Payment Card Industry Security Standards (PCI-DSS) CertificationOn-Demand Courses
CISSPTop Resources
Laptops for IT SecurityWeb Development
Learn to create user-friendly, fast, and dynamic web applications.
View All Web Development Coursesicon-codeBootcamps
Career Kickstarter
6 Months
Best Seller
Full-Stack Developer BootcampJob Oriented
3 Months
Best Seller
UI/UX Design BootcampEnterprise Recommended
6 Months
Java Full Stack Developer BootcampCareer Kickstarter
490+ Hours
Front-End Development BootcampCareer Accelerator
4 Months
Backend Development Bootcamp (Node JS)Skills
ReactOn-Demand Courses
Angular TrainingTop Resources
Top HTML ProjectsBlockchain
Understand how transactions and databases work in blockchain technology.
View All Blockchain Coursesicon-stop-squareBlockchain Certifications
40 Hours
Blockchain Professional Certification32 Hours
Blockchain Solutions Architect Certification32 Hours
Blockchain Security Engineer Certification24 Hours
Blockchain Quality Engineer Certification5+ Hours
Blockchain 101 CertificationOn-Demand Courses
NFT Essentials 101: A Beginner's GuideTop Resources
Blockchain Interview QsProgramming
Learn to code efficiently and design software that solves problems.
View All Programming Coursesicon-codeSkills
Python CertificationInterview Prep
Career Accelerator
3 Months
Software Engineer Interview PrepOn-Demand Courses
Data Structures and Algorithms with JavaScriptTop Resources
Python TutorialWeb Development
4.5 Rating 65 Questions 40 mins read6 Readers

Express.js is a Node.js framework to develop the backend APIs of web and mobile application. It is Open San NPM package and is generally installed after creating a Node.js application.
Node.js itself is a JavaScript framework and is mainly used to develop backend APIs. But it can be used in socket programming to create chat apps. In fact, Node.js can even be used without Express.js to create backend APIs. But the code is very complicated and more difficult to implement.
This is where Express.js shines and does the one job perfectly for which it was created, which is, creating API endpoints. It adds the functionality of middleware and routing with ease in a Node.js application.
Some difference between Express.js and Node.js are -
In this instance, we will write the same app in Express.js and Node.js. First, we will write the app in Node.js. Here, we are using the createServer() method and are writing the response in a template language.
Now, we can go to http://localhost:3000/ and see the output.
Next, we will create the same app in Express.js and see the difference. We don’t need the template code for the entire page. Besides this, the code is much cleaner and easier to understand.
We can go to http://localhost:8080/ and we can see the result.
Since Express.js is a framework for Node.js, the basic requirement is to have Node.js installed on the system. The installation of Node.js can be done in many different ways, depending on the operating system. The best way is to go to the Node.js official site https://Node.js.org/en/ and install it.
Node.js comes with NPM install and it is required, because Express.js is an external node package, which needs to be added in a Node application.
To show how that works, we will create a Node.js application and add Express.js to it. First, create a folder and then make changes to it.
Now, give the command ‘npm init’ to initialize a node app. We can leave most of the questions as default by pressing enter. But we'll give the entry point as server.js. We’ll also assign an author. And finally, enter ‘yes’ for the last question.
This creates a package.json file will in the same directory, and we can install npm packages now.
Then, we’ll add express in our Node.js application by giving the command npm i express from the same directory in the terminal.Next, we’ll create a file server.js in the root directory and add the below code in it. Here, we are first importing express and using it in an app. After that, we’re also declaring a port and making it equal to 3000. Next, we have a GET request to /, where we will send Hello Express to the client.
Finally, we are listening at port 3000 for incoming requests.
Now, we will start the express app by running the command node server.js from the terminal.
Then we will go to http://localhost:3000/ and see the text Hello Express in the browser.
This is a frequently asked interview question on express.js.
Express.js framework is created with JavaScript, and we code which we write with it is also written in JavaScript.Express.js. This is one of the reasons for the rise of MEAN and MERN stack, in which JavaScript knowledge can be used to learn Angular/React at Frontend, MongoDB in the Database and Node.js and Express.js at the backend.
Express has an in-built route support, so a developer can write responses to specific URLs with ease. We can also pass parameters to these routes, which add to the capabilities of the app. Express also supports multiple templating engines, through which you can generate static HTML for a web browser. If you follow this approach, the pages will be generated by the server, and there will be no need to write client code in Angular or React.
Without Express.js, we would have to write complex logic in Node.js, which is both tedious and time-consuming. Express.js code is minimalistic, efficient, and easily understandable by programmers, and because of it easily scalable.
Node.js programs are really fast because they implement event loops and get rid of all inefficiencies. With the power of Node.js and ease of an Express.js, we can build the backend for powerful and scalable web apps in no time.
There are three functions, which are available as arguments in Express.js route handler function. The route handler functions are get, post, put, delete, and others. These are mentioned below.
We will take a small example containing all three arguments. We need to create a Node.js application first by creating a folder and adding node by npm init -y command.
After this, we will install express in it using npm i express command.
Next, we’ll create a file with the name server.js and add the below code in it. Here, we shall first see the use of next() to pass control to the next function. After that with res.send() we will send the text Node.js Developer to the client.
We also have a route to get the id of the user with req.params.id.
Now, start the server with node server.js from the command line and then go to http://localhost:3000/ in the browser. We will see the below messages in the terminal.
Also, if we go to the route like http://localhost:3000/22, we will get the below console log in the terminal.
Expect to come across this popular question in Express.js interview questions for freshers.
We can use Node.js directly for backend programming and there is no need to use Express.js. Actually, Express.js is a Node.js framework and it adds to the ease of programming and scalability of Node.js. We can also code the frontend in JavaScript, without going through frameworks like React and Angular. But we rarely have a large web-app in Vanilla JavaScript.
Similarly, it’s not scalable to write the complete app in Node.js, because the code becomes complex very soon. So, we write the Node.js code with its framework of Express.js. The code for Express.js is very simple, minimalist, scalable, and can be learnt by a JavaScript developer in no time.
So, we don’t use Express.js over Node.js but with Node.js. We take the speed and performance of Node.js along with the simplicity of Express.js to create scalable and performant web-apps APIs. This is the reason, we never use Node.js without Express.js in production and they always go together. Node.js along with Express.js, is part of two popular stocks i.e., MEAN stack and MERN stack.
The growth of Node.js should be credited to Express.js also, which has made Node.js really easy to use. Just as bootstrap led to the success of CSS, similarly Express.js led to the success of Node.js.
Middleware functions are functions that have access to the request and response objects, and the next middleware function in the application's request-response cycle.
Description: Middleware functions can perform tasks like executing code, modifying the request and response objects, ending the request-response cycle, and calling the next middleware function in the stack.
Example:
app.use((req, res, next) => { console.log('Request URL:', req.originalUrl); next();
Error handling middleware is defined by a function with four arguments: err, req, res, and next.
Description: To handle errors, define middleware functions that accept an err argument before req, res, and next.
app.use((err, req, res, next) => { console.error(err.stack); res
Query parameters are accessed using req.query.
Description: Query parameters can be retrieved from the request object using req.query.
Example:
app.get('/search', (req, res) => { const searchQuery = req.query.q; res.send(`Search query: ${searchQuery}`); });
Use body-parser middleware to parse form data.
Description: Form data can be handled by using the body-parser middleware to parse incoming request bodies.
Example:
const bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.post('/submit', (req, res) => { res.send(`Form data: ${req.body.name}`); });
Use body-parser middleware to parse JSON data.
Description: JSON data can be handled by using the body-parser middleware to parse incoming request bodies.
Example:
app.use(bodyParser.json()); app.post('/data', (req, res) => { res.send(`Received JSON data: ${JSON.stringify(req.body)}`); });
As the name suggests the Middleware are used in the Middle of a Request response cycle. In technical terms, they are the function invoked by the Express routing layer, before the final request handler.
Middleware are used to perform the following tasks -
One common rule is that if middleware doesn’t end the request-response cycle, it should call the next middleware function to pass the control. If we don’t do this, the request will be hanging.
We will see a simple middleware in action next. Here, if we have to route /contact, which will first console log the current time. After that it will pass the control to the next function, which actually sends the request back to the user.
Now, when we go to http://localhost:3000/contact we will get the below in console of running node app.
We will also get our response back
There are various types of middleware available in an Express application. They are -
Now, we will look at a simple example of a third-party middleware known as body-parser. It is used to parse requests which have a payload.
We are creating a simple node project and installing the dependencies for express, body-parser and EJS.
Now, we will add a simple form using the templating language of EJS. Create a file SampleForm.ejs and put the below content in it.
Now, in the index.js file we will create a basic Express.js app, without the body parser.
Now, run the app with node index.js command from the terminal and there will be a form to submit in http://localhost:3000/ . When we submit the form, we get the below error, because node cannot parse the json.
Now, we will add a body parser in our index.js file, to solve this issue.

Now, when we submit the form, we get no error and the data is received in the server.

A staple in Express.js interview, be prepared to answer this one.
The method responses or the res object is the response sent by the server to the client. There are many types of responses which can be sent to the client and we will look into them next.
The most common method for res object is the .send(), through which we can send any data to the client. Below is a common example for the same.
Here, if the user goes to the / route in their browser they will be shown the Hello Express text on the browser.
We can also send the status code with the .status() method. These HTTP status codes are very important and interpreted by the client browser as success or failures. In the below example, we have also chained the send method.
There is a shorthand syntax also, which combines both status and send method chaining. The method is called .sendStatus(), and it is used as below.

We can also define headers in our responses to the client and for this we have the .append() method. Below is an example of the same.

The .redirect() method is a very important one, as it can redirect to any route in the web-app. We have to just pass the route as a parameter.

We can also prompt the client to download a file with the .download() method. Below is an example of the same.

Express can connect to different types of databases. It can connect to NoSQL databases and Relational databases also. It connects very well with the two popular databases - MySQL and MongoDB. We will see to connect it to a MongoDB database next.
First, we will go through the basic setup steps and create a new Node.js application by creating a directory and changing to it. Then will initialize a package.json file with the npm init -y command.

Now, we will install the required package of express and also the package of mongoose. Mongoose is a package built for express applications, which makes it very easy to interact with mongoDB databases. We will install them with npm i express mongoose from the command line.

Now, create a file server.js in the root directory and add the below code in it. Here, we are first importing express and mongoose and after that using it in the app. We have also created a PORT and MONGOPORT variable with different values of 3000 and 5000.
After that we are using mongoose.connect() to connect to the local MongoDB instance. If the connection is successful, we are showing a success message or else an error message.
Lastly, we are listening to the connections at PORT 3000.
Now, from the terminal run the command node server.js and we are successfully connected to mongoDB.
We need to show errors or the well-known 404 page to users who go to a non-existent page in our website. We will learn to do that next.
We will first go through the basic setup to create a new Node.js application by creating a directory and changing to it. Then will initialize a package.json file with the npm init -y command. If we don’t specify the -y flag, we need to hit enter to a lot of default fields.

Next, we will make it an express app by adding the express package through npm.
Now, we will create a file server.js in the root directory. Here, we are first doing the import for express and then using it in the app.
Now, we have three routes here, first for the home and second for contact. The third one contains ‘*’ and it contains all the routes not present in the first two. If a user tries to go to some route which is not mentioned, they will be shown ‘Page not Found’.
One thing to keep in mind, is that this wildcard(‘*’) route, should always be the last route in our code.
Lastly, we are listening to all client requests on port 3000.
Now, start the server by giving node server.js in the terminal.
Now, if we go to any route except / or /contact, we will be shown the error message.
CORS stands for Cross-Origin Resource Sharing. It is enabled in the browser by default and because of it a client running on a different network, cannot access the server running on a different network.
Here’s an example. First, we will have our Node runtime installed and created using npm init -y. We will also install express and cors in our Node.js app.
Now, we will have a simple Express server in which we are sending an array of objects to the client, when coming to home root.
First start the node server by running node index.js from the terminal and navigate to http://localhost:3000/ and we will get the array of objects back.
This is allowed because our server and client - both are in localhost. Now, to make the request from a different client network, we have to go to some other site. We are going to https://www.knowledgehut.com/ and opening the console. Here, we will be using the inbuilt fetch api to goto the server endpoint at 3000.
Notice that we are getting a CORS error and not able to get the data.
In order to allow requests to our server endpoint from knowledgehut.com, we need to add it in cors middleware. We have already installed cors earlier, so we are importing it first.
Now, when we go back to knowledgehut.com, and give the same fetch request we will get the data back successfully.