1. The global object
There is an object called global in node. The same object in browser console is called window.
Checkout console.log(global);
in node console.
This object contains functions like setInterval
, setTimeout
etc.
File globaltut.js
:
const interval = setInterval(() => {
console.log('In the interval');
}, 1000);
setTimeout(() => {
console.log("In the timeout")
clearInterval(interval);
}, 5000);
The output will be:
In the interval
In the interval
In the interval
In the interval
In the timeout
Finding the filename and directory name in node
console.log(__dirname);
console.log(__filename);
Some attributes are present in node and not in window and some are present in window and not in node global object.For example 'document' is present in window object and not in node.
2. Modules and Require
We've two files: people.js
and data.js
people.js contains:
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
console.log(`From people.js: ${persons}`);
data.js contains:
const xyz = require('./people');
console.log(`From data.js: `, xyz);
From people.js: arun,shankar,vinod,irfan
From data.js: {}
This way persons array is not available in data.js. We have to manually send from person.js to data.js.
people.js have to contain a line:
module.export = persons;
Then only person is accessible to data.js.
Note: In require statement the path should be a relative path not an absolute path
Exporting multiple things from person.js to data.js
people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];
module.exports = {
personsList: persons,
agesList: ages
}
data.js
const xyz = require('./people');
console.log(xyz.personsList);
console.log(xyz.agesList);
Output on running node data
:
[ 'arun', 'shankar', 'vinod', 'irfan' ]
[ 12, 22, 44, 9 ]
persons
array of people.js --> personsList
array of data.js
To call persons
array of people.js as persons
in data.js:
people.js:
module.export = {
persons: persons,
ages: ages
}
There is a shortcut of these method:
module.export = {
persons, ages
}
Now there are different types of accessing tricks:
Trick 1
//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];
module.exports = {
persons, ages
}
// data.js
const xyz = require('./path_of_people')
console.log(xyz.persons);
console.log(xyz.ages);
Trick 2
//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];
module.exports = {
persons, ages
}
// data.js
const { persons, ages } = require('./path_of_people')
Trick 3
//people.js
const persons = ['arun', 'shankar', 'vinod', 'irfan'];
const ages = [12, 22, 44, 9];
module.exports = {
personsList: persons,
agesList: ages
}
// data.js
const { personsList } = require('./path_of_people')
//agesList is not accessible now. Only personsList is imported here.
node also has some built in modules: For example:
// OS MODULE
const os = require('os');
const os = require('os');
console.log(os.platform());
console.log(os.homedir());
Another important built in module is filesystem module
3. The filesystem
Reading the file
const fs = require('fs');
fs.readFile('./textfile.txt', (err, data) => {
if(err){
console.log('Some error happened');
}else{
console.log(data);
}
})
Output:
<Buffer 6c 6f 72 65 6d 20 69 ... 94 more bytes>
data
is a package of objects. To read it we have to convert it into string.
console.log(data);
//Output: <Buffer 6c 6f 72 65 6d 20 94 more bytes>
console.log(data.toString());
// Output: This is a nice tutorial
Note: readFile
is asynchronous function. Following code reveals that:
const fs = require('fs');
fs.readFile('./textfile.txt', (err, data) => {
console.log(data.toString());
})
console.log('last line of the code')
The output will be:
last line of the code
This is a nice tutorial
Wirting the file
fs.writeFile('./new.txt', 'hello rittwick', () => {
console.log('File was written');
}
writeFile
is also a asynchronous function. This function overwrites the file if the file already exists and creates, writes the file if the file already not exists.
4. Creating and Removing Directories
fs.mkdir('./assets', (err)=>{
if(err){
console.log('Some error happened 1');
}else{
console.log('Folder created');
}
})
mkdir
creates the folder if does not exists. If exists then returns error.
Creating the folder if not exists and deleting otherwise:
if(!fs.existsSync('./assets')){
fs.mkdir('./assets', (err)=>{
if(err){
console.log('Some error happened 1');
}else{
console.log('Folder created');
}
})
}else{
fs.rmdir('./assets', (err) => {
if(err){
console.log('Some error happened 2');
}else{
console.log('Folder Deleted');
}
})
}
5. Deleting Files
if(fs.existsSync('./deleteme.txt')){
fs.unlink('./deleteme.txt', err => {
if(err){
console.log('Some error occurred ');
}else{
console.log('File deleted successful');
}
})
}
unlink
deletes the file if exists otherwise returns an error.
4. Streams and Buffers
Starts using data before it has finished loading completely. For example loading a chunk of data and watching the video before the video fully loads.
Reading a stream
const readStream = fs.createReadStream('./docs/huge.txt');
readStream.on('data', (chunk)=>{
console.log('-------NEW CHUNK---------');
console.log(chunk);
})
This will output some buffers. To get a readable text:
console.log(chunk.toString())
OR there we can directly encode it to a readable string:
const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
Writing a Stream
const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
const writeStream = fs.createWriteStream('./docs/newhuge.txt');
readStream.on('data', (chunk)=>{
writeStream.write('\nNEW CHUNK\n');
writeStream.write(chunk);
})
Piping
Reading streams and writing it into a writestream.
const readStream = fs.createReadStream('./docs/huge.txt', { encoding:'utf8' });
const writeStream = fs.createWriteStream('./docs/newhuge.txt');
readStream.pipe(writeStream);
Top comments (0)