Professional Documents
Culture Documents
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
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
const fs = require('fs')
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
Left Click
Here
ourFirst.js
Type
cmd
Press
Enter
On Keyboard
ourFirst.js
const fs = require('fs')
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')
try {
if (!fs.existsSync(folderName)) {
fs.mkdirSync(folderName)
}
} catch (err) {
console.error(err)
}
const fs = require('fs')
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,
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
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);
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);