Sitemap

NodeJs - Part 1

8 min readSep 24, 2026

--

Press enter or click to view image in full size

JSON and Node.js Core Modules

If you are learning Node.js, you will quickly encounter JSON, Node’s built-in modules, and the http module.

In this tutorial, we will learn:

  1. What JSON is
  2. JSON syntax
  3. JSON objects and arrays
  4. JSON vs JavaScript objects
  5. JSON.stringify()
  6. JSON.parse()
  7. Reading, modifying, and writing JSON data
  8. Node.js Core Modules
  9. The fs, path, os, http, and url modules
  10. How to build a basic server using Node’s http module

1. What is JSON?

JSON stands for JavaScript Object Notation.

It is a lightweight format used to store and exchange data.

You will see JSON everywhere when working with APIs and backend applications.

For example:

{
"name": "John",
"age": 25,
"course": "Node.js"
}

This represents a person with three pieces of information:

  • Name
  • Age
  • Course

Although JSON looks very similar to a JavaScript object, JSON is actually a data format, not JavaScript code.

2. JSON Syntax

JSON uses key-value pairs.

{
"name": "John",
"age": 25
}

The key must be written inside double quotes.

Correct:

{
"name": "John"
}

Incorrect:

{
name: "John"
}

JSON supports these main data types:

  • String
  • Number
  • Boolean
  • Object
  • Array
  • null

Example:

{
"name": "John",
"age": 25,
"isStudent": true,
"skills": ["HTML", "CSS", "JavaScript"],
"address": {
"city": "Enugu",
"country": "Nigeria"
},
"phone": null
}

3. JSON Objects and Arrays

JSON Object

A JSON object is surrounded by {}.

{
"name": "John",
"age": 25
}

Think of it as a collection of related information.

JSON Array

A JSON array is surrounded by [].

[
"HTML",
"CSS",
"JavaScript"
]

An array can also contain objects:

[
{
"name": "John",
"age": 25
},
{
"name": "Jane",
"age": 22
}
]

This is very common when working with APIs.

For example, an API might return:

{
"students": [
{
"name": "John",
"score": 85
},
{
"name": "Jane",
"score": 92
}
]
}

4. JSON vs JavaScript Objects

JSON and JavaScript objects look similar, but they are not the same thing.

JavaScript object

const student = {
name: "John",
age: 25
};

This is a JavaScript object.

JSON

{
"name": "John",
"age": 25
}

This is JSON data.

One important difference is that JSON requires double quotes around keys and strings.

JavaScript also allows single quotes for strings:

const student = {
name: 'John'
};

But JSON requires:

{
"name": "John"
}

JSON is mainly used for storing and transferring data.

JavaScript objects are used directly inside JavaScript programs.

5. JSON.stringify()

Sometimes we have a JavaScript object and want to convert it into JSON.

We use:

JSON.stringify()

Example:

const student = {
name: "John",
age: 25
};

const jsonData = JSON.stringify(student);
console.log(jsonData);

Output:

{"name":"John","age":25}

The important thing here is that student is a JavaScript object, while jsonData is a JSON string.

We can check:

console.log(typeof student);
console.log(typeof jsonData);

Output:

object
string

Why is this useful?

When sending data to an API, we often need to convert our JavaScript object into JSON.

For example:

const user = {
name: "John",
age: 25
};

const data = JSON.stringify(user);

6. JSON.parse()

The opposite of JSON.stringify() is:

JSON.parse()

It converts JSON data back into a JavaScript object.

Example:

const jsonData = '{"name":"John","age":25}';
const student = JSON.parse(jsonData);
console.log(student);

Output:

{ name: 'John', age: 25 }

Now we can access the data normally:

console.log(student.name);

Output:

John

Think of it this way:

JavaScript Object
↓
JSON.stringify()
↓
JSON String

And:

JSON String
↓
JSON.parse()
↓
JavaScript Object

7. Reading JSON Data

Let’s create a JSON file.

Create:

student.json

Inside it, add:

{
"name": "John",
"age": 25,
"course": "Node.js"
}

Now create:

app.js

We can read the JSON file using Node’s fs module.

const fs = require("fs");

const data = fs.readFileSync("student.json", "utf-8");
const student = JSON.parse(data);
console.log(student);
console.log(student.name);
console.log(student.course);

Output:

{
name: 'John',
age: 25,
course: 'Node.js'
}
John
Node.js

What happened?

First, we read the file:

fs.readFileSync("student.json", "utf-8");

Then we converted the JSON string into a JavaScript object:

JSON.parse(data);

Now we can work with the data.

8. Modifying JSON Data

Suppose we want to change John’s age.

const fs = require("fs");

const data = fs.readFileSync("student.json", "utf-8");
const student = JSON.parse(data);
student.age = 26;
console.log(student);

Now the JavaScript object contains:

{
name: "John",
age: 26,
course: "Node.js"
}

But there is an important problem.

We have only changed the JavaScript object in memory.

The actual student.json file has not changed yet.

To save the changes, we need to convert the object back into JSON.

const updatedData = JSON.stringify(student, null, 2);

Then write it back to the file.

fs.writeFileSync("student.json", updatedData);

Complete example:

const fs = require("fs");

const data = fs.readFileSync("student.json", "utf-8");
const student = JSON.parse(data);
student.age = 26;
const updatedData = JSON.stringify(student, null, 2);
fs.writeFileSync("student.json", updatedData);
console.log("Student updated successfully!");

Now open student.json.

You will see:

{
"name": "John",
"age": 26,
"course": "Node.js"
}

9. Writing JSON Data

We can also create a JavaScript object and save it as JSON.

const fs = require("fs");

const student = {
name: "Jane",
age: 22,
course: "Backend Development"
};
const jsonData = JSON.stringify(student, null, 2);
fs.writeFileSync("student.json", jsonData);
console.log("Student saved!");

The file will contain:

{
"name": "Jane",
"age": 22,
"course": "Backend Development"
}

The null, 2 part:

JSON.stringify(student, null, 2);

makes the JSON nicely formatted.

Without it:

{"name":"Jane","age":22,"course":"Backend Development"}

With it:

{
"name": "Jane",
"age": 22,
"course": "Backend Development"
}

10. Node.js Core Modules

Node.js comes with many modules that we can use without installing additional packages.

These are called Core Modules.

Some important ones are:

fs
path
os
http
url

Let’s look at each one.

11. The fs Module

fs means File System.

It allows Node.js to interact with files and folders.

Get Tochukwu Mezue’s stories in your inbox

Join Medium for free to get updates from this writer.

For example:

const fs = require("fs");

We can create a file:

fs.writeFileSync("hello.txt", "Hello from Node.js!");

Read a file:

const data = fs.readFileSync("hello.txt", "utf-8");

console.log(data);

Update a file:

fs.appendFileSync("hello.txt", "\nWelcome to Node.js!");

Delete a file:

fs.unlinkSync("hello.txt");

12. The path Module

The path module helps us work with file and folder paths.

const path = require("path");

Get the file name:

const filePath = "/users/john/documents/app.js";

console.log(path.basename(filePath));

Output:

app.js

Get the directory:

console.log(path.dirname(filePath));

Output:

/users/john/documents

Get the file extension:

console.log(path.extname(filePath));

Output:

.js

This becomes particularly useful when building backend applications that work with files.

13. The os Module

The os module provides information about the operating system.

const os = require("os");

Get the operating system:

console.log(os.platform());

Get the CPU architecture:

console.log(os.arch());

Get the number of CPU cores:

console.log(os.cpus().length);

Get the home directory:

console.log(os.homedir());

You can also get the amount of free memory:

console.log(os.freemem());

14. The url Module

The url module helps us work with URLs.

const url = require("url");

For example:

const myUrl = new URL(
"https://example.com/products?id=25&category=phones"
);

console.log(myUrl.hostname);
console.log(myUrl.pathname);
console.log(myUrl.searchParams.get("id"));

Output:

example.com
/products
25

This becomes useful when handling URLs and query parameters on a server.

15. The http Module

The http module allows us to create an HTTP server.

const http = require("http");

This is where Node.js starts becoming a backend technology.

Let’s create our first server.

16. Building a Server with Node’s http Module

Create a file called:

server.js

Add:

const http = require("http");

const server = http.createServer((req, res) => {
res.end("Hello from my Node.js server!");
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});

Now run:

node server.js

You should see:

Server running on http://localhost:3000

Open your browser and visit:

http://localhost:3000

You should see:

Hello from my Node.js server!

Congratulations, you just created a web server with Node.js.

17. Understanding the Server Code

Let’s break it down.

Step 1: Import http

const http = require("http");

This gives us access to Node’s HTTP functionality.

Step 2: Create the server

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

});

The callback receives two important objects:

req
res

req means request.

It contains information about what the client is requesting.

res means response.

It is what we use to send something back to the client.

Step 3: Send a response

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

This sends a response to the browser.

Step 4: Start listening

server.listen(3000);

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

18. Checking the Request URL

We can inspect the URL the user visits.

const http = require("http");

const server = http.createServer((req, res) => {
console.log(req.url);
res.end("Hello from the server!");
});
server.listen(3000, () => {
console.log("Server running on port 3000");
});

Visit:

http://localhost:3000/about

Your terminal will show:

/about

Visit:

http://localhost:3000/contact

The terminal will show:

/contact

This means we can use the URL to determine what response the server should send.

19. Creating Different Routes

We can create simple routes using req.url.

const http = require("http");

const server = http.createServer((req, res) => {
if (req.url === "/") {
res.end("Welcome to the Home Page");
}
else if (req.url === "/about") {
res.end("This is the About Page");
}
else if (req.url === "/contact") {
res.end("This is the Contact Page");
}
else {
res.statusCode = 404;
res.end("Page Not Found");
}
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});

Now:

http://localhost:3000/

returns:

Welcome to the Home Page

And:

http://localhost:3000/about

returns:

This is the About Page

And:

http://localhost:3000/contact

returns:

This is the Contact Page

Anything else returns:

Page Not Found

20. Sending JSON from a Server

Since we just learned JSON, let’s combine the concepts.

const http = require("http");

const server = http.createServer((req, res) => {
const student = {
name: "John",
age: 25,
course: "Node.js"
};
res.setHeader("Content-Type", "application/json");
res.end(JSON.stringify(student));
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});

When you visit:

http://localhost:3000

the server sends:

{
"name": "John",
"age": 25,
"course": "Node.js"
}

Notice what happened:

const student = {
name: "John",
age: 25,
course: "Node.js"
};

This is a JavaScript object.

We convert it to JSON:

JSON.stringify(student)

Then send it as the HTTP response:

res.end(JSON.stringify(student));

This is the basic idea behind many APIs.

21. Putting Everything Together

Here is a small example combining:

  • http
  • fs
  • JSON
  • routing

Create:

student.json
{
"name": "John",
"age": 25,
"course": "Node.js"
}

Then create:

server.js
const http = require("http");
const fs = require("fs");

const server = http.createServer((req, res) => {
if (req.url === "/student") {
const data = fs.readFileSync("student.json", "utf-8");
res.setHeader("Content-Type", "application/json");
res.end(data);
} else {
res.statusCode = 404;
res.end("Route not found");
}
});
server.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});

Now visit:

http://localhost:3000/student

You will receive the contents of student.json.

22. What You Should Understand After This Lesson

At this point, you should understand the relationship between these concepts:

JSON
↓
Stores and transfers data
↓
JSON.parse()
↓
JavaScript Object
↓
Node.js
↓
Core Modules
↓
http
↓
HTTP Server
↓
Request → Server → Response

The most important concepts to remember are:

JSON

Used for representing and transferring data.

JSON.stringify()

Converts a JavaScript value into a JSON string.

JSON.stringify(data);

JSON.parse()

Converts a JSON string into a JavaScript value.

JSON.parse(data);

fs

Works with files and folders.

path

Works with file and folder paths.

os

Provides information about the operating system.

url

Helps work with URLs and query parameters.

http

Allows us to create HTTP servers.

23. A Simple Mental Model

Think about a backend application like this:

Client
↓
HTTP Request
↓
Node.js Server
↓
Read / Process Data
↓
JSON
↓
HTTP Response
↓
Client

For example:

Browser
↓
GET /student
↓
Node.js
↓
Read student.json
↓
Send JSON response
↓
Browser

--

--