WD Notes
WD Notes
WD Notes
What is Streams?
Streams are objects that let you read data from a source or write data to a
destination in continuous fashion. In Node.js, there are four types of streams −
Why Streams?
handling methods:
transmitted
Types of Streams
File: main.js
1. var fs = require("fs");
6. readerStream.setEncoding('UTF8');
8. readerStream.on('data', function(chunk) {
9. data += chunk;
10. });
11. readerStream.on('end',function(){
12. console.log(data);
13. });
15. console.log(err.stack);
16. });
Now, open the Node.js command prompt and run the main.js
1. node main.js
Node.js Writing to a stream
File: main.js
1. var fs = require("fs");
6. writerStream.write(data,'UTF8');
8. writerStream.end();
12. });
14. console.log(err.stack);
15. });
Now open the Node.js command prompt and run the main.js
1. node main.js
You will see the following result:
Now, you can see that a text file named "output.txt" is created where you had
saved "input.txt" and "main.js" file. In my case, it is on desktop.
Open the "output.txt" and you will see the following content.
Let's take a piping example for reading from one file and writing it to another
file.
File: main.js
1. var fs = require("fs");
8. readerStream.pipe(writerStream);
9. console.log("Program Ended");
1. node main.js
Now, you can see that a text file named "output.txt" is created where you had
saved ?main.js? file. In my case, it is on desktop.
Open the "output.txt" and you will see the following content.
Let's take an example of piping and chaining to compress a file and then
decompress the same file.
File: main.js
1. var fs = require("fs");
4. fs.createReadStream('input.txt')
5. .pipe(zlib.createGzip())
6. .pipe(fs.createWriteStream('input.txt.gz'));
7. console.log("File Compressed.");
Open the Node.js command prompt and run main.js
1. node main.js
Now you will see that file "input.txt" is compressed and a new file is created
named "input.txt.gz" in the current file.
To Decompress the same file: put the following code in the js file "main.js"
File: main.js
1. var fs = require("fs");
4. fs.createReadStream('input.txt.gz')
5. .pipe(zlib.createGunzip())
6. .pipe(fs.createWriteStream('input.txt'));
7. console.log("File Decompressed.");
Open the Node.js command prompt and run main.js
1. node main.js
Node implements File I/O using simple wrappers around standard POSIX
functions. The Node File System (fs) module can be imported using the
following syntax −
var fs = require("fs")
Synchronous vs Asynchronous
Every method in the fs module has synchronous as well as asynchronous
forms. Asynchronous methods take the last parameter as the completion
function callback and the first parameter of the callback function as error. It
is better to use an asynchronous method instead of a synchronous method,
as the former never blocks a program during its execution, whereas the
second one does.
Example
Create a text file named input.txt with the following content −
var fs = require("fs");
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
console.log("Program Ended");
$ node main.js
The following sections provides a set of good examples on major File I/O
methods.
Open a File
Syntax
Following is the syntax of the method to open a file in asynchronous mode −
fs.open(path, flags[, mode], callback)
Parameters
Here is the description of the parameters used −
Flags
Flags for read/write operations are −
1
r
Open file for reading. An exception occurs if the file does not
exist.
2
r+
Open file for reading and writing. An exception occurs if the file
4
rs+
synchronously. See notes for 'rs' about using this with caution.
5
w
Open file for writing. The file is created (if it does not exist) or
6
wx
7
w+
Open file for reading and writing. The file is created (if it does
8
wx+
9
a
Open file for appending. The file is created if it does not exist.
10
ax
11
a+
12
ax+
Like 'a+' but fails if the the path exists.
Example
Let us create a js file named main.js having the following code to open a file
input.txt for reading and writing.
var fs = require("fs");
$ node main.js
Syntax
Following is the syntax of the method to get the information about a file −
fs.stat(path, callback)
Parameters
Here is the description of the parameters used −
Apart from the important attributes which are printed below in the example,
there are several useful methods available in fs.Stats class which can be
used to check file type. These methods are given in the following table.
1
stats.isFile()
2
stats.isDirectory()
3
stats.isBlockDevice()
5
stats.isSymbolicLink()
6
stats.isFIFO()
7
stats.isSocket()
Example
Let us create a js file named main.js with the following code −
var fs = require("fs");
$ node main.js
Writing a File
Syntax
Following is the syntax of one of the methods to write into a file −
This method will over-write the file if the file already exists. If you want to write
into an existing file then you should use another method available.
Parameters
Here is the description of the parameters used −
● path − This is the string having the file name including path.
● data − This is the String or Buffer to be written into the file.
● options − The third parameter is an object which will hold {encoding,
mode, flag}. By default. encoding is utf8, mode is octal value 0666. and
flag is 'w'
● callback − This is the callback function which gets a single parameter
err that returns an error in case of any writing error.
Example
Let us create a js file named main.js having the following code −
var fs = require("fs");
$ node main.js
Reading a File
Syntax
Following is the syntax of one of the methods to read from a file −
This method will use file descriptor to read the file. If you want to read the file
directly using the file name, then you should use another method available.
Parameters
Here is the description of the parameters used −
var fs = require("fs");
var buf = new Buffer(1024);
$ node main.js
Closing a File
Syntax
Following is the syntax to close an opened file −
fs.close(fd, callback)
Parameters
Here is the description of the parameters used −
Example
Let us create a js file named main.js having the following code −
var fs = require("fs");
var buf = new Buffer(1024);
$ node main.js
Truncate a File
Syntax
Following is the syntax of the method to truncate an opened file −
Example
Let us create a js file named main.js having the following code −
var fs = require("fs");
var buf = new Buffer(1024);
$ node main.js
Delete a File
Syntax
Following is the syntax of the method to delete a file −
fs.unlink(path, callback)
Parameters
Here is the description of the parameters used −
Example
Let us create a js file named main.js having the following code −
var fs = require("fs");
There is a very good module for working with file uploads, called "Formidable".
After you have downloaded the Formidable module, you can include the module
in any application:
var formidable = require('formidable');
Upload Files
Now you are ready to make a web page in Node.js that lets the user upload files
to your computer:
Create a Node.js file that writes an HTML form, with an upload field:
Example
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}).listen(8080);
Step 2: Parse the Uploaded File
Include the Formidable module to be able to parse the uploaded file once it
reaches the server.
When the file is uploaded and parsed, it gets placed on a temporary folder on
your computer.
Example
if (req.url == '/fileupload') {
res.write('File uploaded');
res.end();
});
} else {
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}).listen(8080);
The path to this directory can be found in the "files" object, passed as the third
argument in the parse() method's callback function.
To move the file to the folder of your choice, use the File System module, and
rename the file:
Example
Include the fs module, and move the file to the current folder:
var fs = require('fs');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
res.end();
});
});
} else {
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}).listen(8080);
Nodemailer Modules
The Nodemailer module makes it easy to send emails from your computer.
After you have downloaded the Nodemailer module, you can include the module
in any application:
Send an Email
Use the username and password from your selected email provider to send an
email. This tutorial will show you how to use your Gmail account to send an email:
Example
service: 'gmail',
auth: {
user: 'youremail@gmail.com',
pass: 'yourpassword'
});
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
};
if (error) {
console.log(error);
} else {
});
Multiple Receivers
To send an email to more than one receiver, add them to the "to" property of the
mailOptions object, separated by commas:
Example
var mailOptions = {
from: 'youremail@gmail.com',
Send HTML
To send HTML formatted text in your email, use the "html" property instead of the
"text" property:
ExampleSend email containing HTML:
var mailOptions = {
from: 'youremail@gmail.com',
to: 'myfriend@yahoo.com',
Mongo DB
Introduction:
History of MongoDB
MongoDB was created by Eliot and Dwight (founders of DoubleClick) in
2007, when they faced scalability issues while working with relational
database. The organization that developed MongoDB was originally
known as 10gen.
In Feb 2009, they changed their business model and released MongoDB
as an open source Project. The organization changed its name in 2013
and now known as MongoDB Inc.
Features of MongDB
This is how a document looks in MongoDB: As you can see this is similar
to the row in RDBMS. The only difference is that they are in JSON format.
Mongo DB Datatypes
In MongoDB, the documents are stores in BSON, which is the binary
encoded format of JSON and using BSON we can make remote
procedure calls in MongoDB. BSON data format supports various
data-types. Below are the enlisted MongoDB data types:
1. String: This is the most commonly used data type in MongoDB to store
data, BSON strings are of UTF-8. So, the drivers for each programming
language convert from the string format of the language to UTF-8 while
serializing and de-serializing BSON. The string must be a valid UTF-8.
Here, the data type of the value of the name field is a string.
Example: In the following example we are storing the age of the student
in the student collection:
3. Double: The double data type is used to store the floating-point
values.
Example: In the following example we are storing the final result of the
student as pass or fail in boolean values.
5. Null: The null data type is used to store the null value.
Example: In the following example, the student does not have a mobile
number so the number field contains the value null.
6. Array: The Array is the set of values. It can store the same or different
data types values in it. In MongoDB, the array is created using square
brackets([]).
You can also create your own id field, but make sure that the value of
that id field must be unique.
Example: In the following example, when we insert a new document it
creates a new unique object id for it.
Example: In the following example the type of the duration of the project
is undefined.
wrapper.
Example: In the following example we are using all the above method of
the date:
12. Min & Max key: Min key compares the value of the lowest BSON
element and Max key compares the value against the highest BSON
element. Both are internal data types.
Example:
13. Symbol: This data type similar to the string data type. It is generally
not supported by a mongo shell, but if the shell gets a symbol from the
database, then it converts this type into a string type.
Example:
14. Regular Expression: This datatype is used to store regular
expressions.
15. JavaScript: This datatype is used to store JavaScript code into the
document without the scope.
Example: In this example, we are using the JavaScript syntax in the shell:
16. JavaScript with Scope: This MongoDB data type store JavaScript
data with a scope.
Example: In this example, we are using the JavaScript syntax in the shell:
MongoDB Mongo shell is the default client for the MongoDB database
server. It’s a command-line interface (CLI), where the input and
output are all console-based. The Mongo shell is a good tool to
manipulate small sets of data.
The mongo shell gets installed when you install the MongoDB server.
It is installed in the same location as the MongoDB server binary.
Windows
Linux
Mac OS
Once you’ve downloaded and installed MongoDB, you can use the
mongo shell to connect with a MongoDB server that is up and
running.
Copy
net start MongoDB
Copy
Mongo
Now you are in the Mongo shell.
If you want, you can run the mongo and mongod without the
command prompt. To do this, go to the installation location and
double click on the mongod and mongo applications. You will get the
same result as the above.
Different port
The above mongo command only works if your MongoDB server runs
on the default port, which is 27017. If your MongoDB server runs on a
different port, you have to explicitly specify it in the command, as
shown below:
Copy
mongo --port 28010
Remote server
Now it’s time to work with the Mongo shell. First, we will learn some
basic commands that will help you to get started with using
MongoDB.
Run the db command to see the database you are currently working
with
Copy
db
Copy
use company
You can create collections and insert data with the following
command:
db refers to the current database in use.
employee is the collection name.
insertOne is the method to insert a document to the
collection.
Copy
db.employee.insertOne( { name: "mark" } );
Copy
db.employee.find().forEach(printjson)
Copy
show dbs
One important command will help you work with the Mongo shell
easily: the help command. Run the help command to get a list of
help options available in the mongo shell.
Copy
Help
To get a full list of commands that you can execute on the current
database, type db.help()
Disadvantages of the mongo shell
Although the Mongo shell is an excellent tool for learning and testing
the MongoDB server, it is difficult to be used in a production
environment. Being a shell inherently carries certain disadvantages.
They are:
All the versions of MongoDB Compass are opensource (i.e., we can freely
deploy and view the repositories of all MongoDB GUI versions).
Step 1: To download MongoDB Compass, you can use your preferred web
browser, and Open the
https://www.mongodb.com/download-center/compass?jmp=docs page.
Step 2: You need to select the installer and version you prefer. GUI installer are
available as a .exe or .msi package or a .zip archive.
The Compass Home screen displays the details about the MongoDB instance
from which Compass is connected, which includes the connection name, the
deployment type, hostname, and port, version of MongoDB, performance
statistics, and a list of the instance's databases.
When you are connected to the MongoDB Atlas or Mongo Shell, the following
window will appear. Inside this window, you can see the Database tab. The
Database window shows the lists of all the existing databases for your
MongoDB deployment.
On the above window, when you select a database from the given list to view
its collections. You can view database collection when you click the desired
Database in the left navigation pan.
Step 1: Click on the Create Database button from the database tab. It will take
you to the Create Database pop-up dialogue.
Step 2: In the appeared pop-up window, fill the Database and collection
name to create a new database.
Step 3: Finally, click on Create Database button to create the Database and
collection.
Drop a Database in Compass
Step 1: Click on the trash icon that will appear when you hover over the
Database name, after that a confirmation dialog appears.
Step 2: In the pop-up window, enter the name of the Database that you want
to delete.
Step 3: Finally, click Drop Database button to delete the Database you
selected.
Collections in MongoDB Compass
The Collection window displays the list of all the existing collection and views
from the database you have selected. It includes all the name and other
related information for the selected collection or view.
Step 1: In the collection window, click on the bin icon for the collection to
delete. When you click on the trash icon, a dialog appears to ask your
confirmation.
Step 2: In the appeared pop-up dialogue, enter the name of the collection you
want to delete from the database.
● View the documents: The Document tab provides three ways to access
list. In the list view you can easily extend the embedded objects
and arrays.
○ JSON View - In this view documents will be shown as
we use table view, we can easily find out the documents that
● Insert the document: We can have two ways to insert documents into
our collections:
○ JSON Mode: It allows you to write or paste JSON documents in the
editor. You can use this mode to insert one or many documents
interactive way using this editor. It allows you to select all the field
document at a time.
document.
● Clone the documents: You can insert new files and documents by
cloning (i.e. by making an exact same copy of the document). You can
collection.
the tab whether we are viewing our documents in List, JSON, or Table
view.
Introduction to entities of MongoDB:
createDatabase()
Syntax
Basic syntax of use DATABASE statement is as follows −
use DATABASE_NAME
Example
If you want to use a database with name <mydb>, then use DATABASE
statement would be as follows −
>use mydb
switched to db mydb
>db
mydb
If you want to check your databases list, use the command show dbs.
>show dbs
local 0.78125GB
test 0.23012GB
Your created database (mydb) is not present in list. To display database, you
need to insert at least one document into it.
>db.movie.insert({"name":"tutorials point"})
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
In MongoDB default database is test. If you didn't create any database, then
collections will be stored in test database.
Syntax
Basic syntax of dropDatabase() command is as follows −
db.dropDatabase()
This will delete the selected database. If you have not selected any
database, then it will delete default 'test' database.
Example
First, check the list of available databases by using the command, show dbs.
>show dbs
local 0.78125GB
mydb 0.23012GB
test 0.23012GB
>
>use mydb
switched to db mydb
>db.dropDatabase()
>{ "dropped" : "mydb", "ok" : 1 }
>
>show dbs
local 0.78125GB
test 0.23012GB
Collections:
i)createCollection() method with example
Syntax
Basic syntax of createCollection() command is as follows −
db.createCollection(name, options)
collection to be
created
Options Document (Optional) Specify
options about
indexing
Options parameter is optional, so you need to specify only the name of the
collection. Following is the list of options you can use −
also.
also.
capped collection.
While inserting the document, MongoDB first checks size field of capped
collection, then it checks max field.
Examples
Basic syntax of createCollection() method without options is as follows −
>use test
switched to db test
>db.createCollection("mycollection")
{ "ok" : 1 }
>
You can check the created collection by using the command show
collections.
>show collections
mycollection
system.indexes
>db.tutorialspoint.insert({"name" : "tutorialspoint"}),
WriteResult({ "nInserted" : 1 })
>show collections
mycol
mycollection
system.indexes
tutorialspoint
Syntax
Basic syntax of drop() command is as follows −
db.COLLECTION_NAME.drop()
Example
First, check the available collections into your database mydb.
>use mydb
switched to db mydb
>show collections
mycol
mycollection
system.indexes
tutorialspoint
>
>db.mycollection.drop()
true
>
>show collections
mycol
system.indexes
tutorialspoint
>