DEV Community

Rittwick Bhabak
Rittwick Bhabak

Posted on • Updated on

Node.js Crash Course - Part 2 - Node.js Basics

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);
Enter fullscreen mode Exit fullscreen mode

The output will be:

In the interval
In the interval
In the interval
In the interval
In the timeout
Enter fullscreen mode Exit fullscreen mode

Finding the filename and directory name in node

console.log(__dirname);
console.log(__filename);
Enter fullscreen mode Exit fullscreen mode

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}`);
Enter fullscreen mode Exit fullscreen mode

data.js contains:

const xyz = require('./people');
console.log(`From data.js: `, xyz);
Enter fullscreen mode Exit fullscreen mode
From people.js: arun,shankar,vinod,irfan
From data.js:  {}
Enter fullscreen mode Exit fullscreen mode

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;
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

data.js

const xyz = require('./people');
console.log(xyz.personsList);
console.log(xyz.agesList);
Enter fullscreen mode Exit fullscreen mode

Output on running node data:

[ 'arun', 'shankar', 'vinod', 'irfan' ]
[ 12, 22, 44, 9 ]
Enter fullscreen mode Exit fullscreen mode

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
}
Enter fullscreen mode Exit fullscreen mode

There is a shortcut of these method:

module.export = {
    persons, ages
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

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());
Enter fullscreen mode Exit fullscreen mode

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);
    }
})
Enter fullscreen mode Exit fullscreen mode

Output:

<Buffer 6c 6f 72 65 6d 20 69  ... 94 more bytes>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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')
Enter fullscreen mode Exit fullscreen mode

The output will be:

last line of the code
This is a nice tutorial
Enter fullscreen mode Exit fullscreen mode

Wirting the file

fs.writeFile('./new.txt', 'hello rittwick', () => {
    console.log('File was written');
}
Enter fullscreen mode Exit fullscreen mode

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');
        }
    })
Enter fullscreen mode Exit fullscreen mode

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');
        }
    })
}
Enter fullscreen mode Exit fullscreen mode

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');
        }
    })
}
Enter fullscreen mode Exit fullscreen mode

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);
})
Enter fullscreen mode Exit fullscreen mode

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' });
Enter fullscreen mode Exit fullscreen mode

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);
})
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)