Learn Node.js — Beginner Tutorial

Learn Node.js — Beginner Tutorial

Introduction

Node.js is a runtime environment that lets you run JavaScript on the server side — not just in the browser.
It’s fast, scalable, and widely used for building APIs, web servers, and real-time apps.

Prerequisites

Before you start:

  • Install Node.js (v18 or later): https://nodejs.org

  • Install Visual Studio Code or any text editor

  • Basic knowledge of JavaScript

Step 1 — Check Node Installation

Open your terminal or command prompt and run:

node -v npm -v

You should see the installed versions of Node.js and npm (Node Package Manager).

Step 2 — Create Your Project

Create a new folder:

mkdir my-node-app cd my-node-app

Initialize a Node.js project:

npm init -y

This creates a package.json file to manage your project and dependencies.

Step 3 — Create a Simple App

Create a file named app.js:

// app.js console.log("Hello from Node.js!");

Run it:

node app.js

✅ Output:

Hello from Node.js!

Step 4 — Create a Simple HTTP Server

Replace your app.js content with:

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

Run:

node app.js

Open http://localhost:3000 — you’ll see:

Hello, Node.js Server!

Step 5 — Use External Packages

Install a popular package (e.g. Express, a web framework):

npm install express

Update your app.js:

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

Run again:

node app.js

Visit http://localhost:3000 — you’ll see:

Hello Express!

Step 6 — Auto Restart with Nodemon

Install Nodemon globally:

npm install -g nodemon

Run:

nodemon app.js

Now your server restarts automatically whenever you change your code.

Step 7 — Create a Simple API

Add this route or update app.js:

const express = require('express'); const app = express(); const PORT = 3000; app.get('/api/users', (req, res) => { const users = [ { id: 1, name: 'Souy' }, { id: 2, name: 'Leng' }, ]; res.json(users); }); app.get('/', (req, res) => { res.send('Hello Express!'); }); app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); });

Try visiting:
👉 http://localhost:3000/api/users

✅ You’ll see a JSON response!

Step 8 — Summary

You just learned how to:

  • Run JavaScript with Node.js

  • Create an HTTP server

  • Use Express.js

  • Create a REST API

  • Auto-restart with Nodemon

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
close