Node Js Tutorial in Hindi 🔥🔥

2 min read 8 months ago
Published on Nov 04, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Introduction

This tutorial provides a comprehensive guide to Node.js, covering essential concepts and practical applications. Designed for Hindi speakers, it encompasses various modules, server creation, and deployment strategies. By the end, you'll have a solid foundation in Node.js and be ready to create your own applications.

Step 1: Setting Up Node.js

  • Download and install Node.js from the official website.
  • Verify the installation by running the following command in your terminal:
    node -v
    
    This command displays the installed Node.js version.

Step 2: Understanding Node.js Modules

  • Learn about the built-in modules like fs (file system) and os (operating system).
  • To use a module, include it in your code:
    const fs = require('fs');
    const os = require('os');
    

Using the FS Module

  • To read a file:
    fs.readFile('file.txt', 'utf8', (err, data) => {
        if (err) throw err;
        console.log(data);
    });
    
  • To write to a file:
    fs.writeFile('file.txt', 'Hello, Node.js!', err => {
        if (err) throw err;
        console.log('File written successfully!');
    });
    

Step 3: Exploring CommonJS vs ES6 Modules

  • Understand the differences between CommonJS and ES6 module syntax.
  • CommonJS:
    const express = require('express');
    
  • ES6:
    import express from 'express';
    

Step 4: Creating an HTTP Server

  • Set up a basic HTTP server using Node.js:
    const http = require('http');
    
    const server = http.createServer((req, res) => {
        res.statusCode = 200;
        res.setHeader('Content-Type', 'text/plain');
        res.end('Hello World\n');
    });
    
    server.listen(3000, () => {
        console.log('Server running at http://localhost:3000/');
    });
    

Step 5: Working with Express Framework

  • Install Express using npm:
    npm install express
    
  • Create a simple Express application:
    const express = require('express');
    const app = express();
    
    app.get('/', (req, res) => {
        res.send('Hello from Express!');
    });
    
    app.listen(3000, () => {
        console.log('Express server running on http://localhost:3000/');
    });
    

Step 6: Deployment of Node.js Application

  • Choose a hosting platform (e.g., Heroku, Vercel).
  • Prepare your application for deployment
    • Create a package.json file with necessary scripts.
    • Ensure your app listens on the correct port provided by the hosting service.

Conclusion

By following these steps, you've gained essential knowledge about Node.js, including module usage, HTTP server creation, and Express framework basics. You can now explore further by building your applications or diving deeper into more advanced topics. For continued learning, consider exploring additional resources and tutorials available online.