You are on page 1of 27

Node.

js
for
Humans
by
Christopher Topalian
All Rights Reserved Copyright 2021
Dedicated
to
God the Father
Table of Contents
//Search Google for Node.js..............................................4
//Download Node.js Installer.............................................5
//Install Node.js................................................................6
//Install All Needed Tools..................................................7
//Write File.......................................................................8
//How to Run ourFirst.js file..............................................9
//How to Run ourFirst.js file.............................................10
//How to Run ourFirst.js file.............................................11
//The Result of Running ourFirst.js...................................12
//Write File using Append................................................13
//Read a Text File...........................................................14
//Read File of Current Folder...........................................15
//Write File Flags.............................................................16
//Make New Folder..........................................................19
//Read Contents of Current Folder...................................20
//Load Text File to Browser..............................................21
//Load Text File to Browser..............................................22
//See our Text File on our Browser...................................23
//Stop the App in Command Prompt.................................24
//Load Text File to Browser - Better.................................25
//Load Html File to Browser.............................................26

College of Scripting Music & Science


//Search Google for Node.js

1 Node.js

Christopher Topalian

2 Left
Click
//Download Node.js Installer

Christopher Topalian

1 Left
Click

2 Left
Click
//Install Node.js

Download Folder
Christopher Topalian

Node.js
Node Install
Install File
File
//Install All Needed Tools

When Installing, make sure to put a


checkmark in the box that asks if you
would like to install the necessary
tools.
//Write File

const fs = require('fs')

let ourText = 'Hi Everyone'

fs.writeFile('test.txt', ourText,
function(err) {
if (err) {
console.error(err)
return
}
console.log("Works good");
})
//save this script as ourFirst.js
//save it in a folder called ourApp
//How to Run ourFirst.js file

Open the Folder where we saved


ourFirst.js

In the address bar of this folder,


type the letters
cmd

This will open your folder in the command


prompt.

Now, while in the Command Prompt type


node ourFirst.js

then press the Enter button.

This will execute our code!


A text file will be created in our folder!

Details are shown on the next page.


//How to Run ourFirst.js file
Open the Folder where we saved ourFirst.js

Left Click
Here

to highlight the path

ourFirst.js

In the address bar of this folder,


type the letters cmd

Type
cmd

Press
Enter
On Keyboard
ourFirst.js

This will open our folder in the command


prompt. Next we will activate ourFirst.js file.
//How to Run ourFirst.js file
In the Command Prompt, type
node ourFirst.js

then press the Enter button

Notice it states: Works good,


indicating that our text file was created!
Let's check our Folder named ourApp, to see the
new text file, as shown on the next page.
//The Result of Running ourFirst.js

Our text file named test.txt


was created in our folder
named ourApp
//Write File using Append

const fs = require('fs')

let ourText = 'Hi Everyone'

fs.writeFile('test.txt', ourText,
{ flag: 'a+' }, function(err) {
if (err) {
console.error(err)
return
}
console.log("Works good");
})
// a+
// If the text file already exists, it appends to it
// If the text file doesn't exist, it creates it.
//Read a Text File
//Read a Text File and show
contents in the console

const fs = require('fs')

fs.readFile('test.txt', 'utf8',
function(err, data) {
if (err) {
console.error(err)
return
}
console.log(data)
})
//Read Text File of Current Folder

let fs = require('fs');

fs.readFile(process.cwd()
+ "//test.txt", function(err,
data) {
if(err)
console.log(err)
else

console.log(data.toString()
);
});
//Write File Flags

https://nodejs.org/api/
fs.html#fs_file_system_flags

a
Open file for appending.
File is created if it does not exist.

ax
Open file for appending,
but, it fails if the path already exists.

a+
Open file for reading and appending.
File is created if it does not exist.

ax+
Open file for reading and appending.
except it fails if the path already exists.
as
Open file for appending in synch mode.
File is created if it does not exist.

as+
Open file for reading and appending in
synch mode.
File is created if it does not exist.

r
Open file for reading. An exception occurs
if the file does not exist.

r+
Open file for reading and writing. An
exception occurs if the file does not exist.

rs+
Open file for reading and writing in
synchronous mode. Instructs the operating
system to bypass the local file system
cache.
w
Open file for writing.
File is created if it does not already exist
or truncated if it does already exist.

wx
File is created if it does not already exist
except it fails if the path already exists.

w+
Open file for reading and writing.
The file is created if it does not already exist
or
truncated if it does already exists.

wx+
The file is created if it does not already exist
except it fails if the path already exists.
//Make New Folder

const fs = require('fs')

let folderName = 'Our New Folder'

try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName)
}
} catch (err) {
console.error(err)
}

//const folderName = '/Users/energy/test'


//Read Contents of Current Folder

const fs = require('fs')

let folderPath = process.cwd();

let thePath =
fs.readdirSync(folderPath)

console.log(thePath);

//const folderPath =
'/Users/yourName/folderName'
//Load Text File to Browser

let fs = require('fs');
let http = require('http');

let server =
http.createServer(function
(req, res) {
fs.readFile('test.txt',
function (err, data) {
res.end(data);
});
});
server.listen(7000);

//save as textToBrowser.js
//Load Text File to Browser
We run the file named textToBrowser.js in a
similar way as before, but this time we write

node textToBrowser.js

Notice above, that ourApp folder is the


one chosen as the directory,
before we type the command
node textToBrowser.js

press Enter on the Keyboard


This starts a web server on your
computer, which we see on the next
page.
//See our Text File on our Browser
In Any Web browser on our computer,
we type in the address bar
localhost:7000
and then press enter

Notice above,
in our web browser,
the contents of our text file are
shown! (Later we will fix the formatting)
Hi Everyone
//Stop the App in Command Prompt

When in the Command prompt,


to stop a running app we use,
Control + C

On the next page, we will create a server.


Later, to end the server,
we use Control + C when we are in the
command prompt that we used to start it.

Let's first load a text file to a Browser and


check that browser on localhost:7000

This simply means that once the server


is created, we then open any web
browser on our computer,
and type in the address bar
localhost:7000
press enter, and it will then show us the
content of our text file in our Browser!
//Load Text File to Browser - Better

let fs = require('fs');
let http = require('http');

let server =
http.createServer(function
(req, res) {
let stream =
fs.createReadStream('test.txt'
);
stream.pipe(res);
});
server.listen(7000);

//real time streaming of the text file


//Load Html File to Browser

let fs = require('fs');
let http = require('http');

http.createServer(function(request,
response) {
response.writeHead(200,
{'Content-Type': 'text/html'});

let file =
fs.createReadStream('ourFile.html');

file.pipe(response);

}).listen(5000);

console.log('Using port 5000');


Dedicated to God the Father
This book is created by the
College of Scripting Music & Science
Always remember, that each time you write
a script with a pencil and paper, it becomes
imprinted so deeply in memory that the
material and methods are learned extremely
well.
When you Type the scripts, the same is true.
The more you type and write out the scripts
by keyboard or pencil and paper, the more
you will learn programming!
Write and Type EVERY example that you find.
Keep all of your scripts organized.
Every script that you create increases your
programming abilities.
SEEING CODE, is one thing,
but WRITING CODE is another.
Write it, Type it, Speak It, See It, Dream It.
www.CollegeOfScripting.weebly.com

You might also like