๐Ÿ’ป
B.TECH โ€ข SKILL DEVELOPMENT COURSE

Node JS / React JS / Django Lab

Web Development Laboratory โ€ข Experiment 6

๐Ÿ’ป Node JS โš›๏ธ React JS ๐ŸŒ Web Development ๐Ÿงช Lab Program
Experiment 06

Create an XML for the bookstore. Validate the same using both DTD and XSD.

โ† All Lab Programs
EXP-06

☛  Open Terminal or Command Prompt:

Open a terminal or command prompt in the directory where you saved your server.js file.

☛  Run the Server Script:

Execute the server script using the Node.js runtime. In the terminal, run:

node server.js

This will start the HTTP server, and you should see the message "Server running at http://127.0.0.1:3000/".

☛  Access the Server:

Open your web browser and navigate to http://127.0.0.1:3000/ or http://localhost:3000/. You should see the response "Hello, World!".

☛  Check OS Information:

In the same terminal where your server is running, you'll see information about your operating system (OS) type, platform, architecture, CPU cores, etc.

☛  Check Current Working Directory:

The current working directory of the script is printed in the terminal.

☛  Check Joined Path:

The joined path using the path module is printed in the terminal.

☛  Check Custom Event:

The script emits a custom event and listens for it. In the terminal, you should see the message "Custom Event Triggered: { message: 'Hello from custom event!' }".

☛  Stop the Server:

To stop the server, press Ctrl+C in the terminal where the server is running.

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!' });

Output :

In the Terminal:

image

In the Browser:

Link: http://127.0.0.1:3000/

image
โ† Back to Lab Programs ๐Ÿ“š All Lab Manuals
```