0% found this document useful (0 votes)
16 views11 pages

Exercise-6 MST Programs

The document outlines a series of exercises related to Node.js, including creating and running JavaScript files for various functions, setting up a web server, modular programming, and file operations. It provides step-by-step instructions for each exercise, including source code examples and expected outputs. Additionally, it discusses the use of tools like Nodemon for restarting applications and performing file operations such as reading, writing, and appending content to files.
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)
16 views11 pages

Exercise-6 MST Programs

The document outlines a series of exercises related to Node.js, including creating and running JavaScript files for various functions, setting up a web server, modular programming, and file operations. It provides step-by-step instructions for each exercise, including source code examples and expected outputs. Additionally, it discusses the use of tools like Nodemon for restarting applications and performing file operations such as reading, writing, and appending content to files.
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/ 11

Exercise-6 MST Programs

6.a
Course Name: Node.js
Module Name: How to use Node.js
AIM: Verify how to execute different functions successfully in the Node.js platform.

Step 1: Open the VS CODE and create a file fun1.js


Step 2: Open the Terminal (… on the menu bar) in VS CODE.
Step 3: Run the fun1.js using the node command.
PS D:\NodeJS\Exercise-6\6.a >node fun1.js press enter button then output
displayed.

SOURCE CODE: Program1: fun1.js

function tester() {
alert("Hello! Welcome to CSE!");
}
tester();

6.a Program1 OUTPUT:

Step 4: Create a file fun2.js and Run the fun2.js using the node command.
PS D:\NodeJS\Exercise-6\6.a >node fun2.js press enter button then output displayed

Program2: fun2.js
function tester() {
var message;
if (confirm("Press a button!")) {
message = "You pressed OK!";
} else {
message = "You pressed Cancel!";
}
console.log(message);
}
tester();

6.a Program2 OUTPUT:

Note: Functions that perform browser interaction are not compatible with Node.js. You
can try out other functions as well and observe the output.

Step5: Create a file fun3.js and Run the fun3.js using the node command.
PS D:\NodeJS\Exercise-6\6.a >node fun3.js press enter button then output displayed

Program3: Fun3.js

var x = 40; // Declare x, y variables


var y = 20;
function add(num1, num2)
{
return num1 + num2;
}
function multiply(num1, num2)
{
return num1 * num2;
}
function div(num1, num2)
{
return num1 / num2;
}
console.log("Addition of x and y", add(x, y));
console.log("Multiplication of x and y", multiply(x, y));
console.log("Division of x and y", div(x, y));

6.a Program3 OUTPUT:

6.b
Course Name: Node.js
Module Name: Create a web server in Node.js
AIM: Write a program to show the workflow of JavaScript code executable by creating
web server in Node.js.

Step 1: Open the VS CODE and create a file httpserver.js


Step2: Open the Power Shell Terminal(… on the menu bar) in VS CODE.
Step 3: Run the httpserver.js using the node command.
(PS D:\NodeJS\Exercise-6\6.b >node httpserver press enter button then server starts.
Step 4: Open the browser and enter URL as http://localhost:3000 then output displayed.

SOURCE CODE:
//”http” module includes classes, methods and events to create Node.js http server.
// The require() function will return an object, function, property or any other JavaScript type
const http = require("http");
var server = http.createServer((req, res) => {
res.write("Hello World! Welcome to CSE!");
res.end(); // res.end() function is used to end the response process.
});
server.listen(3000); //Create a server that listens on port 3000 of your computer.
console.log("Server started... Running on localhost:3000");
6.b OUTPUT:

6.c
Course Name: Node.js
Module Name: Modular programming in Node.js
AIM: Write a Node.js module to show the workflow of Modularization of Node application.

Step 1: Create a file ModuleApp.js in VS CODE at D:\NodeJS\Exercise-6\6.c > folder.

SOURCE CODE: ModuleApp.js

const http = require("http");


var nodemodule =require("./NodeModule");
var server =http.createServer((request, response) => {
result = nodemodule.authenticateUser("admin", "admin");
response.writeHead(200, { "Content-Type": "text/html" });
response.write("<html><body><h3>" +"Hello World! Welcome to CSE!"+"</h1>
</body></html>");
response.end("<html><body><h3>" + result + "</h1></body></html>");
console.log("Request Received");
});
server.listen(3000);
console.log("Server is running at port 3000");
Step 2: Create a file NodeModule.js in VS CODE at D:\NodeJS\Exercise-6\6.c > folder.

NodeModule.js

exports.authenticateUser = (username, password) => {


if (username === "admin" && password === "admin") {
return "Valid User!";
} else return "Invalid User";
};

Step 3: Run the ModuleApp.js using the node command.


PS D:\NodeJS\Exercise-6\6.c > node ModuleApp press enter button then Server starts.
Step 4: Open a browser and navigate to the URL http://localhost:3000 and display the
output.

6.c OUTPUT:

6.d
Course Name: Node.js
Module Name: Restarting Node Application
AIM: Write a program to show the workflow of restarting a Node application.

SOURCE CODE: restart.js


Step 1: Create a file restart.js in VS CODE at D:\NodeJS\> folder.

const http = require("http");


var server = http.createServer((req, res) => {
res.write("Hello World! I have created my first server!");
res.end();
});
server.listen(3000);
console.log("Server started... Running on localhost:3000");

Step 2: Open the Power Shell Terminal(… on the menu bar) in VS CODE
Step 3: To Restart Node application, we can use the Nodemon tool. To install it in the
application, run the below command.
PS D:\NodeJS\> npm install nodemon -g
 Once the 'nodemon' is installed in the machine, the Node.js server code can be
executed by replacing the command "node" with "nodemon".
Step 4: Run the restart.js using the nodemon command.

PS D:\NodeJS\> nodemon restart.js then server is started as shown below:

Step 5: Open a browser and navigate to the URL http://localhost:3000 and display the
output.

6.d OUTPUT
 Thus, the 'nodemon' starts the application in watch mode and restarts the
application when any change is detected.
 Now open the restart.js application code and do changes in the code .

Modify the code in restart.js file


const http = require("http");
var server = http.createServer((req, res) => {
res.write("Hello! Welcome to CSE\n"); // modified code
res.write("Hello World! I have created my first server!");
res.end();
});
server.listen(3000);
console.log("Server started... Running on localhost:3000");
 nodemon automatically restarted the server by observing changes in the code.

Step 6: Open a browser and navigate to the URL http://localhost:3000 and display the
modified output.

6.d OUTPUT
6.e
Course Name: Node.js
Module Name: File Operations
AIM: Create a text file src.txt and add the following data to it. Mongo, Express, Angular,
Node.

SOURCE CODE:
src.txt
// Create a src.txt file
Welecome to III B.Tech CSE
Client-side Technologies:
HTML
CSS
JS
Anjular
React
Server-side Technologies:
Node
Express
Databases:
MySQL
MongoDB
read.js
// Opening, Reading and Closing a file.
const fs = require("fs");
console.log("Open an existing file");
fs.open('src.txt', 'r', (err, fd) => //Open an existing file
{
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
console.log("Read a file");
fs.readFile('src.txt', 'utf8', (err, fileData) => // Reading a file
{
if (err) throw err;

console.log(fileData);
});
fs.close(fd, function(err) { // Close the opened file.
if (err) {
console.log(err);
}
console.log("File closed successfully.");
});
});

6.e OUTPUT: read.js & src.txt

write.js
// Creating and Writing a file.
var fs = require("fs");
fs.writeFile('input.html', 'utf8', function(err) {
if (err) {
console.error(err);
}
else
console.log("File created successfully!");
});
const fileContent="Welcome to File System in Node.js";
fs.writeFile('input.html', fileContent, function(err){
if (err) {
console.error(err);
}
else
console.log("File conent written successfully!");
});

6.e OUTPUT: write.js

6.e OUTPUT: input.html

append.js
// Append a content to a file
const fs = require('fs');
const appendContent="\nNode.js is a Runtime Environment";
fs.appendFile('input.html', appendContent,function(err){
if (err) {
console.error(err);
}
else
console.log("File content appended successfully!");
});

6.e OUTPUT: append.js

6.e OUTPUT: input.html

You might also like