Your browser does not support JavaScript! Basic Concepts of Node.js Skip to main content

Basic Concepts of Node.js

If you're going to become a full stack or backend developer, then Node.js is the framework you must learn. In this article, I will share some basic concepts of Node.js in simple words.


What is Node.js ?

  • Node.js is a server-side platform.
  • It is built on Chrome's JavaScript runtime.
  • It is used for building fast and scalable network applications.
  • Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.

Basic Concepts of Node.js

Modules

Every module has a default export function call, module.exports = {}, we can include functions or properties inside curly braces for export. We can get rid of implicit declarations.
When we export a user-defined module, we use this syntax: require('./moduleName'), we include './'

Shared State of Modules

When a module is imported into other modules, a single copy of the module is shared with other modules. A change in any property of imported module is reflected in all modules.

Object Factory

Allows exporting an object in other modules so that each module gets its separate copy of the object.
The syntax of object factory:
module.exports = function () {
    return{
       // make an object
    }
};
The syntax of importing is:
var phone = require('./yourModuleName');
myPhone = phone();

Core Modules 

The core modules are built in modules provided by Node.js. We import core modules by this statement
var fs = require('fs');

We don't prefix './' while importing core modules. The variable name is same as the module name, it's the best practice. FileSystem module is used to create and read/write files.

fs.writeFileSync("food.txt","Food is delicious but hot"); //writing file
console.log(fs.readFileSync("food.txt").toString()); //reading file

Another module we have is path module, it is used to normalize file paths. Some operating systems use '/' or '\' in file paths, in order to normalize these symbols, we use path module.

setInterval() vs setTimeout() 

setInterval() calls a function repeatedly after given time.Whereas, setTimeout() schedule a function call at a specified time.

Creating a Basic Server

A simple server is created using Node.js. We import HTTP module and call createServer(), it takes one argument i.e. call back function. This function is called when any user tries to connect to the server. And we call listen() on this method to keep listening to a specified port number. The call back function takes two arguments i.e. request and response. A request is the user request made by browser and response is the result provided by the server to the user. It can be files, text, etc.

Express:

It is a framework for Node.js. A framework provides additional functionalities and features. We do not need to write much code.
In an express project, we have a file named as index.ejs. Here ejs stands for Embedded JavaScript.
We write variable inside this syntax : <%=      %>.

<h1><%= projectName %></h1>
We write any JavaScript code inside this syntax: <%     %>

<ul>
  <% for(var i=0;i<=2;i++){ %>
  <li>Try our service</li>
  <% }%>
</ul>

When you want to use a variable, use = sign (i.e <%=      %>), otherwise for writing JavaScript code don't use it.

Add more Pages

Add new pages to the project by creating a new file in view and routes folder, newfile.ejs and newfile.js, and update app.js file with two lines of code. We write all HTML code inside the ejs file.

Local Variable:

A local variable can be used accross all files in a project.
We make a local variable with this syntax.: app.locals.varname = 10;

When we have code or logic to use in single web page, we write in js file.
When we have code or logic to use across multiple web pages, we write in app.js file.

Conclusion

To conclude, Node.js is a cross-platform JavaScript runtime environment for servers and applications. 
If there is anything I could not clearly explain, just let me know pals. I tried to be as honest in sharing this valuable information as I could. I have shared almost everything I have learned so far.
Do let me know how much did this help you. Peace and blessings buddies. :)

Comments

Related Posts Plugin for WordPress, Blogger...