Mean Stack Development
Mean Stack Development
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.
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.
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.
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) {
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.
Output:
changed 32 packages in 4m
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.
Step 5: You might have observed that the file content is getting replaced with the latest data,
whenever you run this code.
Output: