[go: up one dir, main page]

0% found this document useful (0 votes)
21 views7 pages

Mean Stack Development

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

Mean Stack Development

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

Exercise - 6

5. Course Name: Node.js

a. How to use Node.js Verify how to execute different functions successfully in the
Node.jsplatform.

Step 1: Create a folder NodeJS in D drive and create a new JavaScript file, first.js inside the
folder. Type the below code inside the JavaScript file.

1. console.log("My First Node.js program");

Step 2: Navigate to the created NodeJS folder in the NodeJS command prompt and execute the
JavaScript file, first.js using the node command.

1. node first.js

Step 3: After the successful interpretation of the code, we can see the output in the
Node.js command prompt as shown below.

Node.js command prompt:

Visual Studio Code IDE:

You can also open the NodeJS folder, in Visual Studio Code IDE. To execute the first.js file, open
the integrated terminal and issue the command for executing the file. You will get the output as
shown below:
Let us now see how to execute a JavaScript file that contains code for browser interaction through
Node.js.

We have replaced console.log() with document.write(). The document.write() function displays


output on the browser screen. While interpreting the first.js using Node.js now, we will encounter an
error as shown below:

1. document.write("My first Node.js program!");


6.b Create a web server in Node.js
Write a program to show the workflow of JavaScript code executable by creating web server in
Node.js.
consthttp = require("http");
varserver = 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");

output:
PS C:\Users\srina\.vscode\hello> node httpserver.js
server started... running on localhost:3000
open web browser and enter url as http://localhost:3000
6.c) Write a Node.js module to show the workflow of Modularization of Node
application.Method 1:
Calculator.js –in built
async function add(operator1, operator2) {
return 'Result: ', operator1 + operator2;
}
async function subtract(operator1, operator2) {

return 'Result: ', operator1 - operator2;


}
async function asyncCall() {
console.log('calling');

const result = await add(2, 3);


console.log(result);
}
asyncCall();

output:
PS C:\Users\srina\.vscode\hello> node calc.js
calling
5
6.d Restarting Node Application Write a program to show the workflow of restarting a
Nodeapplication.

NODEMON

It is very easy to get started with this tool. To install it in the application, run the below command.

1. npm install nodemon-g

Output:

PS C:\Users\srina\.vscode\hello>npm install nodemon -g

changed 32 packages in 4m

3 packages are looking for funding


run `npm fund` for details

Once the 'nodemon' is installed in the machine, the Node.js server code can be executed by replacing
the command "node" with "nodemon".

1. nodemon app.js

Thus the 'nodemon' starts the application in watch mode and restart the application when any change
is detected.
6.e) File Operations Create a text file src.txt and add the following data to it. Mongo, Express,
Angular, Node.

Consider the below sample code for reading data from a file "myData.txt".

Step 1: Create a file writefileSys.js and paste the below code in the file.

1. const fs = require('fs');
2. const{promisify}= require('util');
3.
4. constwriteFile=promisify(fs.writeFile);
5.
6. (async()=>{
7. try{
8. awaitwriteFile('myData.txt',`Hey@ ${newDate()}`);
9. console.log('File created successfully with promisify and async/await!');
10. }catch(err){
11. console.log(err);
12. }
13. })();

14.

Step 2: Run the above code using node command 'node fileSystem.js' to see the output.

Step 3: Check in the folder in which "fileSystem" file resides. You can observe that a new file
named myData.txt has been automatically created logging the string content and date.

Step 4: Provide different values in the data/content part of the writeFile() function and observe the
‘myData.txt' file.

awaitwriteFile('myData.txt', `Hey, Hello world`);

Step 5: You might have observed that the file content is getting replaced with the latest data,
whenever you run this code.
Output:

PS C:\Users\srina\.vscode\hello> node writefilesys.js

File created successfully with promisify and async/await!

You might also like