Lesson 2 of 6By Simplilearn
Last updated on Aug 5, 20205049Node.js is open-source and completely free, with countless developers worldwide using it to develop I/O intensive web applications, such as video streaming sites, single-page applications, online chat applications, and other web applications. The platform brings plenty of advantages to the table, making it a better choice than other server-side platforms, such as Java or PHP.
This Node.js Express tutorial is meant to help users with creating servers on Node.js Express, as well as defining API calls that can be connected to the frontend framework, such as React or Angular. First, let’s learn more about what Express.js is and then implement it in a Node.js application to create a server.
We will cover the following topics in this article:
![]() |
|
![]() |
|
The main features of Express.js include:
![]() |
|
![]() |
|
![]() |
|
![]() |
|
Let’s now look at a basic web server that prints “Hello World” on the web browser.
Fig: Express.js Hello World example
Fig: Express.js Hello World output
The following are the steps that we followed in this code to create a basic Express web server:
In this application, we will use Express.js to create a web server and then create API calls. These API calls will handle CRUD operations. CRUD stands for create, read, update, and Delete.
1. Download Node.js from https://nodejs.org/en/download/. Select the appropriate installer for your operation system requirements.
Fig: Node.js Official download page
2. Run the Node.js installer and accept the license agreement. You can leave the remaining settings as default. The installer will install Node.js and prompt you to click on the finish button.
Fig: Node.js Setup dialog box
3. To verify that Node.js was properly installed, open the command prompt and type this command: node --version
Fig: Command Prompt for verification of Node.js
4. When we install Node.js, NPM (Node Package Manager) is also installed. NPM includes several libraries that are used in web applications, such as React. Verify whether it is installed or not with the following command in CMD: npm --version
Fig: Command prompt for NPM verification
Install a text editor of your choice. We are using Visual Studio Code in this tutorial, but you can also use other editors, such as Atom and Sublime Text, if you prefer those.
Fig: Visual Studio Code Official download page
We are using the Postman application to verify the output of the application. We will send and receive API calls and check if the web server is working properly.
To download the application, go to its official website.
Fig: Postman Official download page
Fig: Node.js Express initial setup
In this Node.js Express tutorial, we are going to create our own Node.js application with Express.js, which makes creating a web server much easier than vanilla Node.js.
Let’s first take a look at what the project directory should look like by the end of this tutorial.
Fig: Project directory
This is the file that acts as the main server and binds every API route together. We create this file in the root directory of the project and then add the following code:
const express = require("express");
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use("/api/users", require("./routes/api/users"));
app.listen(3000, () => console.log('Server started'));
This file stores all user data in the user management system. Since this is a small project, we are not using a database here, and only using a list to store the users’ information.
const users = [
{
id: 1,
name: "John",
email: "john@gmail.com"
},
{
id: 2,
name: "Smith",
email: "smith@gmail.com"
},
{
id: 3,
name: "Chris",
email: "chris@gmail.com"
},
{
id: 4,
name: "Jack",
email: "jack@gmail.com"
}
];
module.exports = users;
In this file, we add the API routes that we want to include in the application. First, we’ll create a folder named routes, and inside that, create another folder named api. After creating the folders, add a new file and name it users.js
const express = require("express");
const router = express.Router();
const uuid = require("uuid");
let users = require("../../Users");
router.get("/", (req, res) => {
res.json(users);
});
router.get("/:id", (req, res) => {
const found = users.some(user => user.id === parseInt(req.params.id));
if (found) {
res.json(users.filter(user => user.id === parseInt(req.params.id)));
} else {
res.sendStatus(400);
}
});
router.post("/", (req, res) => {
const newUser = {
id: uuid.v4(),
name: req.body.name,
email: req.body.email
};
if (!newUser.name || !newUser.email) {
return res.sendStatus(400);
}
users.push(newUser);
res.json(users);
});
//Update User
router.put("/:id", (req, res) => {
const found = users.some(user => user.id === parseInt(req.params.id));
if (found) {
const updateUser = req.body;
users.forEach(user => {
if (user.id === parseInt(req.params.id)) {
user.name = updateUser.name ? updateUser.name : user.name;
user.email = updateUser.email ? updateUser.email : user.email;
res.json({ msg: "User updated", user });
}
});
} else {
res.sendStatus(400);
}
});
//Delete User
router.delete("/:id", (req, res) => {
const found = users.some(user => user.id === parseInt(req.params.id));
if (found) {
users = users.filter(user => user.id !== parseInt(req.params.id))
res.json({
msg: "User deleted",
users
});
} else {
res.sendStatus(400);
}
});
module.exports = router;
That’s all the code we needed to create a web server using Express.js in a Node.js application.
Next, we’ll learn how to verify that the web server is working properly. We can start the server by using the following command in the terminal: node index.js
Postman is an intuitive and powerful application that web developers use to check if the API requests in their applications are working properly.
Fig: Postman Application
All API routes should work as expected.
If you have any questions for us, please leave them in the comment section below and we'll have our experts get back to you promptly.
Learn top skills including Angular, Spring Boot, Hibernate, Servlets, and JSPs, as well as MVC, web services, and SOA to build highly web scalable apps with the Full Stack Java Developer Masters Program.
Now that you know how to create a simple web server in this Node.js Express tutorial, you may be wondering how you can obtain the skills necessary to take advantage of its rising popularity. Fortunately, there are some great options to learn this exciting and practical skill set at your own pace. Simplilearn’s Node.js Certification training course will give you a great foundation in this popular platform, combining live, instructor-led training, self-paced tutorials, and hands-on projects to help you become career-ready upon completion. Get started today and seize your future!
Name | Date | Place | |
---|---|---|---|
Node.js Training | 5 Feb -5 Mar 2021, Weekdays batch | Your City | View Details |
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.
Node.js Training
Full Stack Java Developer
*Lifetime access to high-quality, self-paced e-learning content.
Explore CategoryNode.js MySQL Tutorial: Create and Manage an Employee Database
An Introduction to Project Management: A Beginner’s Guide
Working With MERN (MongoDB, ExpressJS, ReactJS, and NodeJS) Stack
React With Redux Tutorial: Learn the Basics
JavaScript DOM Tutorial: 2020 Edition
The Ultimate Guide to Creating Chatbots