Learn Node JS from scratch | Powerful Backend Development 🔥

Learn Node JS from scratch | Powerful Backend Development 🔥

13.7K views
Summary
Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project!

Learn About Node.js: Key Syntaxes, Libraries, and Concepts

Node.js has transformed the way we build scalable network applications, bringing JavaScript to the server-side. This article delves into the core syntaxes, libraries, and essential concepts that make Node.js a powerful tool for developers.

Introduction to Node.js

Node.js is a runtime environment that allows developers to execute JavaScript code outside of a web browser. It is built on Chrome's V8 JavaScript engine and uses an event-driven, non-blocking I/O model, which makes it lightweight and efficient. Node.js is ideal for building data-intensive real-time applications that run across distributed devices.

✅ Ryan Dahl is the founder of Node.js . He developed Node.js as he was frustrated with the limitations of the Apache HTTP server and wanted to simplify web application development . Node.js was released in 2009, and since then, it has become one of the most popular JavaScript runtime environments used for server-side development.

Node.js Vs Javscript

JavaScript (JS) is a programming language used for client-side scripting on the web, game development, and mobile app development. Node.js, on the other hand, is a JavaScript runtime environment built on Chrome's V8 JavaScript engine that allows developers to run JavaScript on the server-side.
Key differences:
  • Client-side vs Server-side: JavaScript is primarily used for client-side scripting, while Node.js is used for server-side development.
  • Runtime Environment: JavaScript is run on web browsers or mobile app runtimes, whereas Node.js is a standalone runtime environment.
  • Functionality: JavaScript is used for dynamic web pages, form validation, and interactive client-side functionality. Node.js is used for server-side logic, database integration, and API connectivity.

Core Syntaxes in Node.js

Modules

Modules are a fundamental part of Node.js, allowing developers to organize code into reusable components. Node.js has a built-in module system, and each file in a Node.js application is considered a module.

const fs = require('fs'); // Importing the file system module
const http = require('http'); // Importing the HTTP module

Creating a Simple Server

One of the first things developers learn in Node.js is how to create a simple server. Here’s a basic example:

const http = require('http');

const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello, World!\n');
});

const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}/`);
});

Callback Functions

Node.js heavily relies on callback functions to handle asynchronous operations. For instance, reading a file in Node.js can be done as follows:

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    console.log(data);
});

Important Libraries in Node.js

Express.js

Express.js is a fast, unopinionated, and minimalist web framework for Node.js. It provides a robust set of features for building web and mobile applications. Here’s an example of a simple Express server:

const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
    res.send('Hello, Express!');
});

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

Mongoose

Mongoose is an Object Data Modeling (ODM) library for MongoDB and Node.js. It provides a straight-forward, schema-based solution to model your application data. Here’s how you can define a simple schema and model:

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: String,
    email: String,
    password: String,
});

const User = mongoose.model('User', userSchema);

Socket.io

Socket.io enables real-time bidirectional event-based communication. It works on every platform, browser, or device, focusing equally on reliability and speed. Here’s a basic example:

const io = require('socket.io')(3000);

io.on('connection', (socket) => {
    console.log('a user connected');
    socket.on('disconnect', () => {
        console.log('user disconnected');
    });
});

File System (fs) Module

The fs module allows you to interact with the file system. You can read, write, delete, and manipulate files and directories. Here are some examples:

Reading a File

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error(err);
        return;
    }
    console.log(data);
});

Writing to a File

const fs = require('fs');

const content = 'Some content!';

fs.writeFile('example.txt', content, err => {
    if (err) {
        console.error(err);
        return;
    }
    console.log('File has been written');
});

Deleting a File

const fs = require('fs');

fs.unlink('example.txt', err => {
    if (err) {
        console.error(err);
        return;
    }
    console.log('File deleted');
});

Path Module

The path module provides utilities for working with file and directory paths. It’s particularly useful for ensuring cross-platform compatibility of file paths.

Join Paths

const path = require('path');

const filePath = path.join(__dirname, 'public', 'images', 'image.jpg');
console.log(filePath);

Resolve Path

const path = require('path');

const absolutePath = path.resolve('public/images', 'image.jpg');
console.log(absolutePath);

OS Module

The os module provides operating system-related utility methods and properties. This is useful for retrieving information about the operating system.

Get System Information

const os = require('os');

console.log('OS Platform:', os.platform());
console.log('OS CPU architecture:', os.arch());
console.log('Number of CPU cores:', os.cpus().length);
console.log('Total Memory:', os.totalmem());
console.log('Free Memory:', os.freemem());

Essential Concepts in Node.js

Event Loop

The event loop is what allows Node.js to perform non-blocking I/O operations. Understanding the event loop is crucial to mastering Node.js. It handles callbacks and continues to execute the rest of the code while I/O operations are being performed in the background.

Asynchronous Programming

Node.js's asynchronous nature is one of its core strengths. It allows for the efficient handling of multiple operations simultaneously. Promises and async/await are modern techniques to manage asynchronous code, providing a cleaner and more readable approach compared to callbacks.

const fetchData = async () => {
    try {
        const response = await fetch('https://api.example.com/data');
        const data = await response.json();
        console.log(data);
    } catch (error) {
        console.error('Error fetching data:', error);
    }
};

fetchData();

Streams

Streams are a powerful way to handle reading/writing files, network communications, or any kind of end-to-end information exchange in an efficient manner. There are four types of streams in Node.js: readable, writable, duplex, and transform streams.

Buffer

The Buffer class in Node.js is designed to handle binary data directly. This can be used to manipulate data streams such as TCP streams, file system operations, and others.

const buffer = Buffer.from('Hello, World!', 'utf8');
console.log(buffer.toString('hex'));
console.log(buffer.toString('base64'));

Building a Simple REST API with Node.js

Let’s combine some of the concepts and libraries discussed to build a simple REST API using Express.js and Mongoose.

Setup and Configuration

First, set up a new Node.js project and install the necessary dependencies:

npm init -y
npm install express mongoose body-parser

Connecting to MongoDB

const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost:27017/mydatabase', {
    useNewUrlParser: true,
    useUnifiedTopology: true,
});

const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', () => {
    console.log('Connected to MongoDB');
});

Defining a Schema and Model

const userSchema = new mongoose.Schema({
    name: String,
    email: String,
    password: String,
});

const User = mongoose.model('User', userSchema);

Creating Express Routes

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;

app.use(bodyParser.json());

app.post('/users', async (req, res) => {
    const user = new User(req.body);
    try {
        await user.save();
        res.status(201).send(user);
    } catch (error) {
        res.status(400).send(error);
    }
});

app.get('/users', async (req, res) => {
    try {
        const users = await User.find({});
        res.send(users);
    } catch (error) {
        res.status(500).send(error);
    }
});

app.listen(PORT, () => {
    console.log(`Server running at http://localhost:${PORT}/`);
});

Advanced Topics in Node.js

Cluster Module

The cluster module allows you to create child processes (workers) that share the same server port. This can help you take advantage of multi-core systems to handle the load more efficiently. Here’s an example:

const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;

if (cluster.isMaster) {
    console.log(`Master ${process.pid} is running`);

    // Fork workers
    for (let i = 0; i < numCPUs; i++) {
        cluster.fork();
    }

    cluster.on('exit', (worker, code, signal) => {
        console.log(`Worker ${worker.process.pid} died`);
    });
} else {
    http.createServer((req, res) => {
        res.writeHead(200);
        res.end('Hello, World!\n');
    }).listen(8000);

    console.log(`Worker ${process.pid} started`);
}

Child Processes

The child_process module allows you to spawn child processes. This can be useful for executing shell commands or running scripts. Here’s an example of how to use the exec function:

const { exec } = require('child_process');

exec('ls -l', (err, stdout, stderr) => {
    if (err) {
        console.error(`exec error: ${err}`);
        return;
    }
    console.log(`stdout: ${stdout}`);
    console.error(`stderr: ${stderr}`);
});

Testing in Node.js

Testing is a crucial part of any application development process. Node.js has several libraries to facilitate testing. Some popular ones are Mocha, Chai, and Jest.

Using Mocha and Chai

Here’s an example of a simple test using Mocha and Chai:

const { expect } = require('chai');

describe('Array', () => {
    describe('#indexOf()', () => {
        it('should return -1 when the value is not present', () => {
            expect([1, 2, 3].indexOf(4)).to.equal(-1);
        });
    });
});

Using Jest

Jest is another popular testing framework. Here’s how you can write a simple test:

test('adds 1 + 2 to equal 3', () => {
    expect(1 + 2).toBe(3);
});

â–¶â–¶â–¶Node.Js Vs Php


Feature Node.js PHP
Target Audience Full-stack developers Wide range of audiences
Frameworks Express, Meteor, Koa, Nest Laravel, CodeIgniter, Symfony, CakePHP
Learning Curve Easy for JavaScript learners Easy to learn, challenging to master
Speed and Performance High-speed execution, non-blocking I/O Slower, synchronous code implementation
Security Vulnerable to XSS, data leaks, CSRF Vulnerable to security risks due to low entry barrier
Syntax JavaScript PHP syntax, similar to C and Perl
Platform Cross-platform Cross-platform
Usage Real-time applications, APIs, microservices Web development, content management systems, e-commerce

 

Conclusion

Here are five popular websites that use Node.js:
  1. Netflix: Netflix uses Node.js for its backend services, enabling fast and scalable streaming of movies and TV shows.

  2. Uber: Uber employs Node.js for handling real-time data processing and management in their platform, supporting millions of concurrent users.

  3. LinkedIn: LinkedIn utilizes Node.js for its mobile backend services, providing real-time updates and interactions to its users.

  4. Walmart: Walmart uses Node.js to handle their server-side operations, enabling efficient handling of inventory and e-commerce transactions.

  5. PayPal: PayPal has integrated Node.js into its platform for handling their backend services, ensuring fast and reliable payment processing.

These websites leverage Node.js for its scalability, performance, and ability to handle large volumes of concurrent connections effectively.

Node.js is a powerful tool for building scalable and efficient network applications. Its non-blocking, event-driven architecture makes it an excellent choice for real-time applications. By mastering its core syntaxes, important libraries, and essential concepts, developers can harness the full potential of Node.js. Whether you are building a simple server, a REST API, or a complex real-time application, Node.js provides the tools and flexibility needed to succeed.

We’ve covered a broad range of topics from basic syntax to advanced concepts, providing you with a comprehensive understanding of Node.js. This knowledge, combined with practical experience, will empower you to create robust and high-performance applications.

Happy coding!