NA +91-8727899942 thecoderjob@gmail.com

CHAPTER - 9

Some core modules example in node Js.

Here are some list od modules : 

1. http Module : The http module provides functionality to create an HTTP server and make HTTP requests. Below is an example of creating an HTTP server

2. fs Module : The fs module provides file system related operations. Here's an example of reading a file asynchronously

3. path Module :  The path module provides utilities for working with file and directory paths. Below is an example of joining paths

4. os Module : The os module provides operating system-related utility methods. Here's an example of getting the CPU architecture

5. events Module : The events module provides an EventEmitter class that allows you to emit and listen for events. Below is an example of using EventEmitter

Here's is the Code !!

//Http module
const http = require('http');

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

const PORT = 3000;
server.listen(PORT, () => {
    console.log(`Server is running on port ${PORT}`);
});

//Fs module
const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
    if (err) {
        console.error('Error reading the file:', err);
        return;
    }
    console.log('File content:', data);
});

//Path module
const path = require('path');

const fullPath = path.join(__dirname, 'folder', 'file.txt');
console.log('Full path:', fullPath);

//Os module
const os = require('os');
console.log('CPU architecture:', os.arch());

//Event module
const EventEmitter = require('events');

class MyEmitter extends EventEmitter {}

const myEmitter = new MyEmitter();

myEmitter.on('event', (data) => {
    console.log('Event data:', data);
});

myEmitter.emit('event', 'Hello, EventEmitter!');

Chapter 8

Understanding modules and require in node Js

Previous chapter

Chapter 10

What is callbacks in node js

Next chapter

Get In Touch

NA

thecoderjob@gmail.com

+91-8727899942

Popular Links

© www.thecoderjob.com. All Rights Reserved.               Designed by HTML Codex