How to Create a Simple Server Using ExpressJS?
Last Updated :
23 Jul, 2025
The server plays an important role in the development of the web application. It helps in managing API requests and communication between the client and the backend. ExpressJS is the fast and famous framework of the Node.Js which is used for creating the server.
In this article, we will create a simple server using the ExpressJS.
What is the Server?
A server is a program that listens to client requests and responds with data, such as web pages or APIs. Servers are essential for hosting websites, handling user requests, and serving dynamic content.
ExpressJS simplifies server creation in Node.js by providing an easy-to-use API for handling requests, responses, and middleware
- Fast & Lightweight: Express makes building servers quick and efficient.
- Simplified Routing: Easily handle different URL paths and HTTP methods.
Steps to Create a Simple Server Using ExpressJS
Follow these steps to create a basic Express server with an HTML page.
Step 1: Create a Project Folder
Open your terminal or VS Code and create a new folder named root. Navigate inside it.
mkdir express-app
cd express-app
Step 2: Initialize a Node.js Project
Run the following command to create a package.json file, which manages dependencies.
npm init -y
Package.JsonStep 3: Install ExpressJS
Install Express as a dependency to set up your server.
npm i express
Dependencies:
"dependencies": {
"express": "^4.18.2",
}
Step 4: Create the Server File
Create a file named server.js in your project directory and add the following code
JavaScript
const express = require('express');
const app = express();
const PORT = 3000;
app.get('/', (req, res) => {
res.send('<h1>Hello, Geeks!</h1><p>This is your simple Express server.</p>');
});
app.listen(PORT, () => {
console.log(`Server is listening at http://localhost:${PORT}`);
});
Step 5: Start the Server
In the terminal, start your server with the following command:
node server.js
Output
Express Server- Import Express: Loads the Express module.
- Create an App Instance: Initializes the Express application.
- Define a Port: Sets the port number to 3000.
- Set Up a Route: Defines a route for the root URL (/) that sends an HTML response.
- Start the Server: Begins listening on the specified port and logs a message to the console.
Best Practices for Creating a Simple Server Using ExpressJS
- Use Environment Variables: Store configuration details like port numbers and database URLs in environment variables to enhance security and flexibility.
- Implement Error Handling: Use middleware to manage errors gracefully, ensuring the server remains stable and provides informative responses.
- Log Requests and Errors: Implement logging to monitor server activity and troubleshoot issues effectively.
Similar Reads
Creating Socket.IO Server using Express Generator Socket.IO is a library for real-time communication between the server and the client. In Socket.IO, the headers are shared only once and it also works on the top of the TCP layer. Web Sockets are the base of the Socket.IO library. It is easier to implement the Socket.IO in express applications that
3 min read
How To Create a Simple HTTP Server in Node? NodeJS is a powerful runtime environment that allows developers to build scalable and high-performance applications, especially for I/O-bound operations. One of the most common uses of NodeJS is to create HTTP servers. What is HTTP?HTTP (Hypertext Transfer Protocol) is a protocol used for transferri
3 min read
Create an Express server with EJS Templating Engine EJS (Embedded JavaScript) is a simple and popular templating engine for JavaScript. it is designed to generate HTML markup with plain JavaScript. EJS is commonly used in server-side web applications built with Node.js. It allows you to generate dynamic HTML pages. In this article, we will create stu
3 min read
How to use Template Engines in Express JS ? Express.js is a popular web framework for Node.js that simplifies the process of building web applications. One of the key features of Express is its ability to integrate with template engines, allowing developers to dynamically generate HTML pages with data from their server. In this article, we'll
3 min read
How to run ExpressJS server from browser ? ExpressJS is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Typically, ExpressJS servers are run on a Node.js environment on the backend. However, for development, testing, or educational purposes, you might want to ru
2 min read
How to Create a Simple Server in Node.js that Display Hello World ? We will create a simple server in Node.js that returns Hello World using an express server. Node.js is a powerful JavaScript runtime built on Chrome's V8 engine, commonly used to build scalable network applications. One of the fundamental tasks when learning Node.js is creating a simple server that
2 min read