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:

const http = require('http'); const server = http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World using Node.js!'); }); server.listen(3000, () => { console.log('Server running on http://localhost:3000'); });

Here, you have to manually handle routing, headers, request methods, and responses. For large applications, this becomes messy.

Express.js server (simplified):

const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Hello World using Express.js!'); }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });

👉 Much shorter and cleaner. This is why Express is used by companies like Uber, Accenture, and IBM.


Prerequisites

Before starting, make sure you have:

  1. Node.js installed. Download here.

    • Check with: node -v

  2. npm (Node Package Manager) (comes with Node.js).

  3. A text editor like VS Code.

  4. Basic knowledge of JavaScript.


🛠 Step 1: Initialize a New Node.js Project

Create a new folder for your project and initialize it:

mkdir hello-express cd hello-express npm init -y

This will generate a package.json file with default settings.


🛠 Step 2: Install Express.js

Run the following command to install Express:

npm 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:

const express = require('express'); const app = express(); // Home route app.get('/', (req, res) => { res.send('Hello World from Express!'); }); const PORT = 3000; app.listen(PORT, () => { console.log(`Server is running on http://localhost:${PORT}`); });

🛠 Step 4: Run Your Application

Start your server with:

node app.js

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:

app.get('/about', (req, res) => { res.send('This is the About Page'); }); app.get('/contact', (req, res) => { res.send('Contact us at contact@example.com'); });

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.

  1. Create a folder called public.

  2. Put a file index.html inside it.

<!DOCTYPE html> <html> <head> <title>Hello Express</title> </head> <body> <h1>Welcome to Express.js!</h1> <p>This is a static HTML page served by Express.</p> </body> </html>
  1. Update your app.js:

app.use(express.static('public'));

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:

npm install express

2. “Port already in use”

Another program is using port 3000. Run:

npx kill-port 3000

Or change your port in app.listen().

3. Server not restarting after changes

By default, Node doesn’t auto-restart. Install nodemon:

npm install -g nodemon nodemon app.js

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