0% found this document useful (0 votes)
3 views3 pages

Program 9th Node Js Program For 2nd Year B.tech

The document outlines a Node.js server implementation using the HTTP and OS modules, along with event handling through EventEmitter. It creates a simple server that responds with 'Hello, World!' and logs various OS information and the current working directory. Additionally, it demonstrates joining paths and emitting a custom event with a message.

Uploaded by

mdateeq807
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Program 9th Node Js Program For 2nd Year B.tech

The document outlines a Node.js server implementation using the HTTP and OS modules, along with event handling through EventEmitter. It creates a simple server that responds with 'Hello, World!' and logs various OS information and the current working directory. Additionally, it demonstrates joining paths and emitting a custom event with a message.

Uploaded by

mdateeq807
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Server.

js
// Step 1: Import required modules

const http = require('http');

const os = require('os');

const path = require('path');

const { EventEmitter } = require('events');

// Step 2: Create an instance of EventEmitter

const eventEmitter = new EventEmitter();

// Step 3: Create a simple HTTP server

const server = http.createServer((req, res) => {

res.writeHead(200, { 'Content-Type':'text/plain' });

res.end('Hello, World!\n');

});

// Step 4: Define server port and hostname

constport = 3000;

consthostname = '127.0.0.1';

// Step 5: Listen for requests on the specified port and hostname

server.listen(port, hostname, () => {

console.log('Server running at http://${hostname}:${port}/');

});

// Step 6: Print OS information

console.log('OS Type:', os.type());

console.log('OS Platform:', os.platform());


console.log('OS Architecture:', os.arch());

console.log('CPU Cores:', os.cpus().length);

// Step 7: Print current working directory

console.log('Current Working Directory:', process.cwd());

// Step 8: Join paths using the path module

const joinedPath = path.join(__dirname, 'public', 'images');

console.log('Joined Path:', joinedPath);

// Step 9: Handle a custom event

eventEmitter.on('customEvent', (data) => {

console.log('Custom Event Triggered:', data);

});

// Step 10: Emit a custom event

eventEmitter.emit('customEvent', { message:'Hello from custom event!' });

In the Terminal:
In the Browser:

Link: http://127.0.0.1:3000/

You might also like