WT Unit-2 Node Js
WT Unit-2 Node Js
(2101CS304)
Unit-02
NodeJS
Introduction to NodeJS
NodeJS Modules
Node Package Manager
Creating Web Server
File System
Events
Traditional Programming Techniques
Traditional programming does I/O the same way as it does local function calls: Processing
cannot continue until an operation finishes.
This model of blocking when doing I/O operations derives from the early days of time
sharing systems in which each process corresponded to one human user.
With the widespread use of computer networks and the Internet, this model of “one user, one
process” did not scale well.
Multi-Threading programming is one alternative to this programming model, A thread is a
kind of lightweight process that shares memory with every other thread within the same
process, problem with multi-threading is programmer need to synchronize threads.
This query requires that the current thread or process wait until the database layer finishes processing it.
In event-driven systems, this query would be performed in this way:
1 query_finished = function(result) {
2 do_something_with(result);
3 }
4 query('SELECT * FROM posts WHERE id = 1', query_finished);
In this technique query will send query to database and will process other task until database finishes
processing it and will call query_finished function when processing is done.
If above command returns some version information it means you have node installed in your system.
If command returns error stating ‘node’ is not recognized as internal or external command it simply means
you don’t have node installed yet.
Save the above file in a specific directory and navigate to that directory in the
terminal/command prompt.
Example:
1 var http = require('http');
Note: we are going to explore many core modules in details later in this unit.
Now we can use circle module in other files 1 var cir = require('./circle');
2 console.log(cir.area(10))
by importing the module using require
method. We can omit .js in file path but
relative path (./) is mandatory.
1 node app.js
To run the program we can simply use
command prompt and fire the node
app.js command in the same directory.
Prof. Arjun V. Bala #2101CS304 (WT) Unit 02 – NodeJS 16
Loading a Folder Module
You can use the path for a folder to load a module similar to the file module like this
Syntax
1 var myModule = require('./myModulePath');
Here,
first node will try to find the myModulePath.js file in current directory, if file exists it will import that file
If myModulePath.js does not exist in current directory it will try to find package.json file in
myModulePath folder, It will then parse the package.json file and find “main” attribute’s value as a relative
path for the entry point.
If package.json not found in the folder it will consider index.js file as default “main” attribute value.
You can use basic operators like <,>,<= and >= while specifying version,
npm install package-name@"<0.3"
OR
npm install package-name@">=0.3 <0.5"
Updating a module
If you want to update locally installed package,
npm update package-name
}
Prof. Arjun V. Bala #2101CS304 (WT) Unit 02 – NodeJS 22
Loading from the node_modules folder
When we download/install packages using NPM, it will be stored in a folder named
node_modules.
To load such packages we can use require method with absolute path.
If provided path is absolute and not a core Node module, Node will try to find it inside the
node_modules folder. Here absolute path (no ./ in beginning) is
mandatory to load form node_modules folder
Syntax
1 var thirdPartyModule = require('thirdPartyModule');
If Node fails to find the file, it will look inside the parent folder called
../node_modules/thirdPartyModule.
If it fails again it will try the parent folder and keep descending until it reaches the root or
finds the required module.
The fs.stat() method is used to return information about the given file or directory. It returns
an fs.Stat object which has several properties and methods to get details about the file or
directory.
stat
1 const fs = require('fs');
2 fs.stat('index.js', (err,data) => {
3 console.log(data);
4 });
Note: it is better to use asynchronous approach for GUI based application as it will not block the
execution.
Prof. Arjun V. Bala #2101CS304 (WT) Unit 02 – NodeJS 28
“fs” Core Module (Cont.)
To delete a file with the File System module, use the fs.unlink() method.
unlink
1 const fs = require('fs');
2 fs.unlink('darshan.txt', (err) => {
3 if(err){ throw err }
4 console.log("File Deleted");
5 });
To rename a file with the File System module, use the fs.rename() method.
rename
1 const fs = require('fs');
2 fs.rename('darshan.txt', 'darshan_uni.txt', (err) => {
3 if(err){ throw err }
4 console.log("File Deleted");
5 });
app.js
1 const child_process = require('child_process');
2 child_process.exec('dir',(err,stdout,stdin)=>{
3 console.log(stdout)
4 })
Above command will print ‘Server running at http://127.0.0.1:3000’, which means our first node server is
ready to serve, just use any browser and open the above URL to see Hello World