Menu

Skill Development Course - NodeJs Lab Manual - (LAB PROGRAMS)



Notice: Undefined index: title in /home/u681245571/domains/studyglance.in/public_html/labprograms/nodejsdisplay.php on line 89

Aim:

 

Solution :

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

Related Content :

Skill Development Course - NodeJs Lab Programs

1) Build a responsive web application for shopping cart with registration, login, catalog and cart pages using CSS3 features, flex and grid.   View Solution

2) Use JavaScript for doing client – side validation of the pages implemented in the experiment   View Solution

3) Explore the features of ES6 like arrow functions, callbacks, promises, async/await. Implement an application for reading the weather information from openweathermap.org and display the information in the form of a graph on the web page.   View Solution

4) Develop a java stand alone application that connects with the database (Oracle / mySql) and perform the CRUD operation on the database tables.   View Solution

5) Create an xml for the bookstore. Validate the same using both DTD and XSD.   View Solution

6) Create a custom server using http module and explore the other modules of Node JS like OS, path, event.   View Solution

7) Develop an express web application that can interact with REST API to perform CRUD operations on student data. (Use Postman)   View Solution

8) Create a service in react that fetches the weather information from open weathermap.org and the display the current and historical weather information using graphical representation using chart.js   View Solution

9) Create a TODO application in react with necessary components and deploy it into github.   View Solution