How to Create a “Hello World” Application in Node.js with Express.js
If you’re starting your journey with Node.js, one of the best ways to learn is by creating a simple “Hello World” application. In this guide, you’ll learn step by step how to build your first web server using Express.js, one of the most popular frameworks for Node.js.
By the end, you’ll not only see “Hello World” on your screen but also understand why Express makes web development so much easier.
📌 What is Express.js?
Express.js is a fast, lightweight, and flexible web framework for Node.js. It simplifies the process of building web applications and APIs by providing built-in features like:
-
✅ Easy routing (handling different URLs)
-
✅ Middleware (functions that process requests)
-
✅ Error handling
-
✅ Integration with databases and templates
👉 In simple words: Express saves you from writing hundreds of lines of code in raw Node.js and replaces them with just a few lines.
⚡ Why Use Express Instead of Plain Node.js?
To understand the power of Express, let’s compare both approaches.
Plain Node.js server:
Here, you have to manually handle routing, headers, request methods, and responses. For large applications, this becomes messy.
Express.js server (simplified):
👉 Much shorter and cleaner. This is why Express is used by companies like Uber, Accenture, and IBM.
✅ Prerequisites
Before starting, make sure you have:
-
Node.js installed. Download here.
-
Check with:
node -v
-
-
npm (Node Package Manager) (comes with Node.js).
-
A text editor like VS Code.
-
Basic knowledge of JavaScript.
🛠 Step 1: Initialize a New Node.js Project
Create a new folder for your project and initialize it:
This will generate a package.json
file with default settings.
🛠 Step 2: Install Express.js
Run the following command to install Express:
This will add Express as a dependency in your project.
🛠 Step 3: Create Your Server File
Create a new file named app.js
and add this code:
🛠 Step 4: Run Your Application
Start your server with:
Open your browser and go to 👉 http://localhost:3000
You should see:
Hello World from Express! 🎉
🌐 Step 5: Add More Routes
Express makes it super easy to add new routes. Let’s add an About page:
Now visit http://localhost:3000/about
or http://localhost:3000/contact
.
👉 This is called routing. You can define as many routes as you want.
🎨 Step 6: Serve Static Files (HTML, CSS, JS)
Express can also serve static files like HTML, CSS, and images.
-
Create a folder called
public
. -
Put a file
index.html
inside it.
-
Update your
app.js
:
Now, when you visit http://localhost:3000
, Express will show the index.html file.
⚠️ Common Errors Beginners Face
1. “Cannot find module ‘express’”
This means Express isn’t installed. Run:
2. “Port already in use”
Another program is using port 3000. Run:
Or change your port in app.listen()
.
3. Server not restarting after changes
By default, Node doesn’t auto-restart. Install nodemon:
This restarts the server automatically on changes.
📌 Next Steps
Now that you have a Hello World app, here’s what to learn next:
-
Add POST routes to handle form submissions.
-
Use middleware like
body-parser
for JSON data. -
Connect Express with MongoDB (popular stack: MERN).
-
Deploy your app on Heroku / Vercel / Render.
💡 Final Thoughts
You’ve just built your first Node.js + Express.js application 🎉. With just a few lines of code, you created a working server that responds to requests.
The power of Express lies in its simplicity + scalability. From here, you can expand into full-stack apps, APIs, or even production-ready enterprise applications. Keep practicing, and soon you’ll move from a Hello World to building real-world projects.
Comments
Post a Comment