Learning backend development becomes much easier when you start writing actual programs.

If you already understand some JavaScript, Node.js gives you an opportunity to use that knowledge to create applications that run outside the browser.

In this tutorial, we’ll walk through the basic steps of creating a Node.js project and building a simple application.

You don’t need an advanced understanding of Node.js. Basic JavaScript knowledge is enough to follow along.

What Do You Need Before Starting?

Before creating your first Node.js application, you should have:

  • Basic JavaScript knowledge
  • A computer
  • Node.js installed
  • A code editor
  • Access to a terminal or command prompt

You can verify whether Node.js is installed by opening your terminal and running:

node --version

You should see a Node.js version printed in the terminal.

You can also check npm:

npm --version

If both commands return version numbers, your environment is ready for the next step.

Create a New Project Folder

Create a folder for your application.

For example:

my-first-node-app

Open the folder in your terminal:

cd my-first-node-app

The exact command depends on where you created the folder.

Initialize the Node.js Project

Now run:

npm init

npm will ask you several questions about the project.

For a quick setup, you can use:

npm init -y

This creates a package.json file automatically.

What Is package.json?

The package.json file contains important information about a Node.js project.

It can contain details such as:

  • Project name
  • Version
  • Description
  • Entry point
  • Scripts
  • Dependencies
  • Development dependencies

A simple package.json might look similar to:

{
    "name": "my-first-node-app",
    "version": "1.0.0",
    "description": "My first Node.js application",
    "main": "app.js",
    "scripts": {
        "start": "node app.js"
    }
}

As your project grows, this file becomes an important part of managing the application.

Create Your First JavaScript File

Create a file called:

app.js

Add this code:

console.log("Hello from my first Node.js application!");

Save the file.

Now run:

node app.js

You should see:

Hello from my first Node.js application!

Congratulations!

You have just executed your first Node.js application.

Create a Simple HTTP Server

Now let’s make the application a little more interesting.

Replace the contents of app.js with:

const http = require("node:http");

const server = http.createServer((req, res) => {
    res.writeHead(200, {
        "Content-Type": "text/plain"
    });

    res.end("Hello from my Node.js server!");
});

server.listen(3000, () => {
    console.log("Server is running at http://localhost:3000");
});

Run:

node app.js

You should see:

Server is running at http://localhost:3000

Now open your browser and visit:

http://localhost:3000

You should see:

Hello from my Node.js server!

Node.js includes an HTTP API that allows applications to create HTTP servers and work with requests and responses.

Understanding the Example

Let’s break down the code.

Importing the HTTP Module

const http = require("node:http");

Node.js includes several built-in modules.

The node:http module provides functionality for HTTP-related operations.

Node.js documentation recommends the node: prefix for built-in modules in modern code.

Creating the Server

const server = http.createServer((req, res) => {
    // server logic
});

The createServer() function creates an HTTP server.

The callback receives two important objects:

  • req — information about the incoming request
  • res — used to send a response

Sending a Response

Inside the callback:

res.writeHead(200, {
    "Content-Type": "text/plain"
});

This sets the HTTP status and response content type.

Then:

res.end("Hello from my Node.js server!");

finishes the response and sends text back to the browser.

Starting the Server

Finally:

server.listen(3000, () => {
    console.log("Server started");
});

This tells the server to listen for connections on port 3000.

Understanding the Request

When you open:

http://localhost:3000

your browser sends an HTTP request to the Node.js server.

The application receives that request:

Browser
   ↓
HTTP Request
   ↓
Node.js Server
   ↓
HTTP Response
   ↓
Browser

The browser then displays the response.

This request-and-response cycle is one of the fundamental concepts behind web development.

Creating Different Responses

You can check the requested URL using req.url.

For example:

const http = require("node:http");

const server = http.createServer((req, res) => {

    if (req.url === "/") {
        res.end("Welcome to my Node.js application!");
        return;
    }

    if (req.url === "/about") {
        res.end("This is the About page.");
        return;
    }

    res.writeHead(404);
    res.end("Page not found.");
});

server.listen(3000, () => {
    console.log("Server running on port 3000");
});

Now you can visit:

http://localhost:3000/

or:

http://localhost:3000/about

Each URL can return a different response.

This is a basic example of routing.

Add an npm Start Script

Instead of typing:

node app.js

every time, you can add a script to package.json:

{
    "scripts": {
        "start": "node app.js"
    }
}

Then run:

npm start

npm will execute the command defined in the start script.

This becomes especially useful as projects become larger and require multiple development commands.

Understanding the Basic Project Structure

At this stage, your project might look like:

my-first-node-app/
│
├── app.js
├── package.json
└── package-lock.json

The files have different responsibilities.

app.js

Contains your application’s JavaScript code.

package.json

Contains project configuration and dependency information.

package-lock.json

Records the dependency tree used by npm so installations can be reproduced more consistently.

What Should You Learn Next?

Once your first Node.js application is working, you can start learning more practical backend concepts.

A useful learning path is:

Step 1: Node.js Fundamentals

Learn:

  • Modules
  • npm
  • Events
  • File system
  • HTTP
  • Environment variables

Step 2: Build APIs

Learn how to create:

  • GET endpoints
  • POST endpoints
  • PUT/PATCH endpoints
  • DELETE endpoints

Step 3: Learn Express.js

Express.js can make API and web server development more convenient by providing a higher-level framework around Node.js HTTP capabilities.

Step 4: Add a Database

You can connect your Node.js backend to databases such as:

  • MySQL
  • PostgreSQL
  • MongoDB

Step 5: Add Authentication

Once you understand APIs and databases, you can learn:

  • Login systems
  • Password hashing
  • Sessions
  • JWT
  • Authorization
  • Role-based access

Step 6: Deploy Your Application

Finally, learn how to deploy Node.js applications to a server or cloud platform.

A Simple Beginner Project

After completing this tutorial, try building a small Task API.

For example:

GET    /tasks
POST   /tasks
GET    /tasks/:id
PUT    /tasks/:id
DELETE /tasks/:id

You can initially store the data in memory.

Later, connect the application to MySQL or another database.

This progression will help you understand how real backend applications are structured.

Common Beginner Mistakes

When starting with Node.js, avoid trying to learn everything simultaneously.

Some common mistakes include:

Trying Too Many Frameworks

Start with Node.js fundamentals before jumping between multiple frameworks.

Copying Code Without Understanding It

Code examples are useful, but try to understand what each line does.

Ignoring Error Messages

Terminal errors are valuable clues. Read them carefully rather than immediately searching for a complete replacement solution.

Building Projects That Are Too Large

Start with small projects.

A simple API that you fully understand is more valuable than a large application copied from a tutorial.

Final Thoughts

Getting started with Node.js doesn’t require a complicated project.

Create a folder, initialize npm, write a JavaScript file, run it, and gradually turn that simple program into a small server.

Once you understand how requests and responses work, you can move toward APIs, databases, authentication, and complete backend applications.

The most important step is to keep building.

Write a small amount of code, run it, break it, fix it, and learn from the process.

Share.
Leave A Reply

Exit mobile version