Meaningful intro to NodeJS

Meaningful intro to NodeJS

I am a fellow developer, and I created this blog after using Node.js extensively. If I had to teach Node.js to my younger self, this is probably how I would introduce it.

disclamer → this is a beginners intro for someone who quickly wants to move towards more important things in MERN.

what is nodeJS?

It is basically Chrome’s V8 engine taken and packaged to use on our server. Hence, it doesn’t have things like window and DOM, as the programming here will not revolve around browsers.

lets you use JS outside browser.(JS runtime envirnoment)

Here, I will not be demonstrating the installation process. You can install Node.js from its official site or use platforms like GitHub Codespaces, Gitpod, etc.

Things you’ll learn →

  • Intialising Project

  • Basics of JavaScript

  • Modules

  • File Handling (fs Module)

  • Architecture of Node.js

  • Coming soon → HTTP module & URL module


Intialising Project →

First we have to create package.json file. it is kind of a configuration file which holds all the dependencies of our projects. For beginners ,its an important file similar to settings in your phone.

npm init -y

such a file will be created.


Basics of JavaScript →

To proceed further and get meaningful output from this blog, you should know functions and understand concepts like variables, conditionals (if-else & switch), data types, strings, objects,asynchronous programming etc.


Modules →

It’s a very common practice to code in smaller bits and use them as a whole, aka modular programming. Knowing how to import and export modules will be enough to proceed.

how to export/import a function?

Let us assume we have created this simple JS code.

Here, I have created index.js and hello.js, with index.js exporting the greet function to hello.js.

index.js code →

function greet(a) {
    return `hello ${a}!!! `;
}
module.exports = greet ;

hello.js code →

const greet = require("./index")
console.log("hello world");
console.log(greet("john"))

File Handling →

creating file

Creating File AsyncronouslyCreating File Syncronously
fs.writeFile("./path/file","data-inside..",callback function());fs.writeFileSync("./path/file","data-inside..");
needs a callbackcallback is not required

here the first parameter is the path of the file.

Example → if i want to create names.txt in a folder of path ./path then first parameter should be ./path/names.txt

the second parameter is the data you want to be init and the last one is a call back function.

reading file

Reading File AsyncronouslyReading File Syncronously
callback fn is required-
fs.readFile(“./path/file“,”file-encoding”,callback function())fs.readFileSync("./path/file","file-encoding");
example → fs.readFile("./hero.txt","utf-8",(err,result)=>{ if(err) console.log(“error”,err); return console.log(result);})example → fs.readFileSync("./hero.txt","utf-8");

There are additional functions, such as copy, delete (unlink), and append, which can be learned progressively by working on projects.


→ Node.js Architecture

as we know we first send a request to the server.In our nodeJS built server, all the requests/tasks enter in an event queue.This event queue as the name suggest is a queue of tasks.Tasks from this queue is monitored by event loop. If the task is sync, then it is sent to a thread pool.This thread pool has threads which are fixed for the machine hardware.The thread pool assigns a thread to work on the task and returns response on completion.Async task is the non-blocking code which is completed in background.


Conclusion → you can read my other blogs for more quick info about topics. Thanks for Reading :)