Node.js Document

From PheonixSolutions
Jump to navigation Jump to search

NODEJS

  • One of the best source for backend development as we need not learn any new programming language because it runs on Java script.

The power of the command line and how to use Node

[edit]
  • To create first node file using command prompt or terminal on required location for example Desktop(am using desktop) on your PC/laptop move to desktop location in terminal by entering cd desktop .
  • To create a file type “mkdir filename”( I type intro-to-node as file name) and enter you will find file on your desktop.
  • Now to create a .js file in above created file type echo > file.js( here index.js) and you should be able to find it.
  • Now open the index.js file in VS Studio Code and type some javascript code and console and run it on terminal by entering node index.js , now you will be able to view the output we entered in studio code.

The Node REPL( Read Evaluation Print Loop)

[edit]
  • Just type node and you will be into it so that you can type code in terminal and execute there itself.
  • To move out of Node enter .exit and you will be back and to clear all the stuff you have gone till now just enter cls and check terminal/prompt will be cleaned.

How to use the native node modules ?

[edit]
  • Move to https://nodejs.org/ so that we can find all modules involved in node.
  • To access a module like “fs, crypto,etc;” (here I use fs for example) we need to use require function as shown below;

const fs = require(‘fs’); which we can be found in above link.

  • Remember other than the modules included with/ came up with node we have external modules also.
    the-npmnode-package-manager-and-installing-external-modules.
    • To install external modules move to your project file directory and type npm init in terminal and you will be able to find that package.json file gets introduced to your project file folder and you can give required information, then enter and type yes/y to confirm.

    • https://www.npmjs.com/ - Use this link to search for different packages and it’s usage is also found (required API or code for using the package) and the process is as follows.

    • Search for any package you need in above mentioned link( here let us type cricket in search column then all packages regarding cricket are found and decently click on the required package).

    • You will be shown the process to install the package here it shows as type “npm install cricket” in terminal (need to type exact name as it is shown there).

    • Now when you go back to vs studio code you will be able to find a new file

”package-lock.json” is introduced mentioning all required dependencies and also we can find new package we installed is shown in package.json file as dependencies.

  • Now give code accordingly the usage and run the file in terminal by entering

“node index.js”(actually file name here my filename is index.js) we should be able to find required content in server side i.e; in terminal.

Express.js with Node.js

What is express?

[edit]
  • Express is a framework which is widely used alongside Node.js just to easy backend development.

Creating our first server with Express.

[edit]
  • By following above mentioned steps add a folder my-express-server using terminal/prompt and create server.js file and then initialise npm with server.js as starting point(which mean that package.json file would be created).
  • Now we need to install express just by using command npm install express(if u don’t wanna include express as dependency i.e to use temporarily, use npm install express –no-save) and then u will find it in dependencies in package-lock.json file.
  • const express = require("express");
  • const app = express();
  • The above shown code is the usage with EXPRESS and to create our server.
  • const express = require("express");
  • const app = express();
  • app.listen(3000, function(){
  •     console.log('server started on port 3000');
  • });
  • When we run the above code in terminal we would be able to view the console message we entered, but if we run on localhost:3000 in browser we get an error “cannot GET /”because we didn’t send any response from our server to port 3000. Now arises the concept of responses and requests.
  • NOTE: 3000 mean a port number and listen is one of the methods of express.
  1. Handling Requests and Responses: the GET request

    [edit]
    const express = require("express");
    const app = express();
    app.get("/", function(req, res){
        res.send("Helo buddy!");
    });
        app.listen(3000, function(){
             console.log('server started on port 3000');
    });
    
    • In the above code if u find we added get request for server to send request to browser(port3000) by using send method(like listen, send is also a method).
    • NOTE: req implies Request, similarly res for Response.
    • Now when we browse localhost:3000 we get response we entered.

    Understanding and working with different routes.

    [edit]
    In the above example we used home route/home page in app.get request(i.e “/”) now if we want to another route/page in same server we can add it as shown below.
    const express = require("express");
    const app = express();
    
    app.get("/", function(req, res){
        res.send("Helo buddy!");
    });
    app.get("/about", function(req, res){
        res.send("Am an Indian!");
    });
    app.listen(3000, function(){
        console.log('server started on port 3000');
    });
    
    Now when we go to localhost:3000/about route/page we get above mentioned response, in the same way we can add various routes to a port.
    Always after making changes to go back to directory from server in terminal we need to click ctrl+c, but if we install nodeman module it automatically moves to directory after we makes changes in the code…use npm install -g nodeman.
    

    Responding to Requests with HTML files.

    [edit]
    • ***Note that “__dirname” gives u the file path when run on server.
    • Console.log(__dirname) gives u path of the directory where you are.
    • const express = require("express");
    • const app = express();
    • app.get("/",  function(req, res){
    •     res.sendFile(__dirname + "/index.html");
    • })
    • app.listen(3000, function(){
    •     console.log('server started on port 3000');
    • });
    • //BELOW SHOWN CODE IS INDEX.HTML code WHICH WE INCLUDED WITH SERVER.JS
    • <html lang="en" dir='ltr'>
    • <head>
    •     <meta charset="UTF-8">
    •     <meta name="viewport" content="width=device-width, initial-scale=1.0">
    •     <title>calculator</title>
    • </head>
    • <body>
    •     <form action="index.html" method="post">
    •         <input type="text" name="num1" placeholder="firstNumber">
    •         <input type="text" name="num2" placeholder="secondNumber">
    •         <button type="submit" name="submit">calculate</button>
    •     </form>
    •     
    • </body>
    • </html>
    • We added html file using terminal/prompt command “echo >” as shown in document.
    • After running code, when user navigates to port 3000, user should be able to view the inputs and button shown in html code. But when clicked on calculate button user would not be able to find result and this gives us the concept of body-parser.

    Processing POST requests with BODY-PARSER

    [edit]
    • Bodyparser parses the request we sent…..it might be bodyparser.text which parse all requests to text….it might be bodyparser.json which is a special case….it might be bodyparser.urlencoded which we are using for above example as we need to parse data from html form.
    • const express = require("express");
    • const bodyparser = require("body-parser");
    • const app = express();
    • app.use(bodyparser.urlencoded({extended: true}));
    • app.get("/",  function(req, res){
    •     res.sendFile(__dirname + "/index.html");
    • });
    •  app.post("/", function(req, res){
    •      var num1 = Number(req.body.n1);
    •      var num2 = Number(req.body.n2);
    •      var result = num1 + num2;
    •      res.send("the result of calculation is" + result);
    •  });
    • app.listen(3000, function(){
    •     console.log('server started on port 3000');
    • });
    • // index.html code
    • <html lang="en" dir='ltr'>
    •     <head>
    •         <meta charset="UTF-8">
    •         <meta name="viewport" content="width=device-width, initial-scale=1.0">
    •         <title>calculator</title>
    •     </head>
    •     <body>
    •         <form action="/" method="post">
    •             <input type="text" name="n1" placeholder="firstNumber">
    •             <input type="text" name="n2" placeholder="secondNumber">
    •             <button type="submit" name="submit">calculate</button>
    •         </form>
    •         
    •     </body>
    •     </html>
    • ***Now when we run above code in port 3000, user is asked to enter input values in index.html which is called by server.js and displayed in browser using GET and POST helps in displaying data provided in it as shown above.
    • In the above code if we don’t use bodyparser, req.body.n1… then we get undefined result in the browser, so bodyparser does great help by sending request to browser which runs on server side which is damn advantageous.

    Application Programming Interfaces (APIs)

    What is an API?

    [edit]
    • An API is set of commands, protocols, functions and objects that programmers can use to create a software or interact with an external system/server.
    • Example: Requesting from ur server to someone else’s server and getting response from that server to ur server happens because of some interaction or some interface which stays b/w two servers and this thing we name it as an API.

    API Endpoints, Paths & Parameters!!

    [edit]
    • Consider an example of a XYZ API, it has an endpoint (can be url), when we enter that url/endpoint it does GET request from XYZ API and displays the response accordingly.

      [edit]
    • Now coming to path it comes after url with slash(/) i.e; the options we found in XYZ API when we enter this url (added path) it navigates to required page accordingly.

    • Now coming to Parameters, these come after path and starts with ? and key value pair(if no name for parameter then starts with contains = “some value” after ?). If we have more than one parameter then each parameter is divided with & (nw this will be a new url);Order we arrange parameters doen’t matter.

    • ***Note: Look for a practical example in browser to understand easily.

    What is PostMan?

    [edit]
    • It doesn’t look cool to always go and browse a given url with endpoints, paths & parameters in a browser, then comes concept of Postman.
    • We can make same GET request with url , add all paths and parameters easily rather in browser(we have separate sections for key - value pair , etc) and after entering complete url and sending GET request we get data displayed in pretty good manner rather in browser.
    • Because we get Data in JSON format.

    What is JSON?

    [edit]
    • JSON is abbreviated as JavaScript Object Notation.
    • We can have formats like XML, HTML, etc; also for getting data from an API, but JSON is damn advantageous than any format.
    • We can collapse data easily which allows us to consume space(major adv of JSON).

    Making GET requests with Node https module:

    [edit]
    • After we get an url with exact endpoints, path & parameters(with API keys) now we use in below code format to make GET requests with Node https module.
    • const express = require('express');
    • const { response } = require('express');
    • const app = express();
    • app.get("/", function(req, res){
    •    const url = 'https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=dcde0cd2783e74f6da23c32c10dde009';
    •    https.get(url, function(response{
    •        console.log(response);
    •    
    •     res.send("server is up")
    • })
    • app.listen(3000, function(){
    •     console.log("server is running on port 3000");
    • })
    • appid term in above mentioned url, is meant to be API key.
    • Till ORG it is endpoint, data/2.5/weather is path.
    • weather?q=London,uk&appid=dcde0cd2783e74f6da23c32c10dde009
    • The above story are parameters and each parameter is divided by &.

    How to Parse JSON?

    [edit]
    const express = require('express');
    const { response } = require('express');
    const app = express();
    app.get("/", function(req, res){
    

     const url = 'https://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=dcde0cd2783e74f6da23c32c10dde009';  https.get(url, function(response){  console.log(response.statuscode);

    response.on("data", function(data){
    

     const weatherdata = JSON.parse(data)  const temp = weatherdata.main.temp

    Console.log(temp);
    

      })

     })
    

      res.send("server is up")

     })
     app.listen(3000, function(){
     console.log("server is running on port 3000");
    

    })

    • If we try to understand above code we have used JSON.parse to convert data in string form to form which look alike as Javascript and point to be noted is everything will be shown on our server only info which we send by using res.send will be displayed in browser.
    • const temp = weatherdata.main.temp
    • Console.log(temp);
    • The above code is to know info of temp and main.temp is it’s path in JSON code.
    • We can also stringify data which is in Javascript form using JSON.stringify , it’s opposite to JSON.parse.s

    NOTE: ****Only one res.send can be written, if we want multiple lines or images or data to be sent to browser then we write res.write(image code or lines code or data code) in different lines and then we execute by writing code as res.send().

    This we can find it as important if we are sending data to display on browser.

    MongoDB

    What is MongoDB actually and process to install?

    [edit]
    • Database are mainly of two types 1. SQL and 2. NoSQL.
    • SQL is defined as Standard Query Language and data based on tabular basis use this kind of database.
    • But NoSQL database provides a mechanism for storage and retrieval of data that is modeled in means other than tabular relations used in relational databases and this kind of database is more advantageous.
    • Now MongoDB is NoSQL kind of database which stores data in JSON format.
    • Now we can install mongoDB from https://www.mongodb.com/ and select MSI package and install it with installation wizard (Install MongoDB compass can be avoided).
    • Navigate to C drive of your system and create a new folder ”data” and create another folder “db” in “data” folder.
    • Now open GITBASH change directory to cd ~ and enter echo > .bash_profile.
    • Now enter vim .bash_profile and we navigate to vim editor, then enter “i” to get into insert mode and check your mongoDB installed version (for me it’s 4.2).
    • Now enter

    Alias mongod="/c/Program\files/MongoDB/Server/4.0/bin/mongod.exe"

    alias mongo="/c/Program\ Files/MongoDB/Server/4.0/bin/mongo.exe"

    • Hit ESC key and type “wq!” to save and exit vim; now re-launch bash/terminal and check for mongoDB version using command “mongo --version”…we will find mongoDB got installed in ur system.

    MongoDB CRUD operations.

    [edit]
    • Move to GITBASH and enter “mongo” you will be inside mongo directory and enter “show dbs” which shows u different databases in your system and you will find “admin, local and config” databases with 0.00 GB data by default.
    • To create a database(db) we need to enter use nameDB ( name indicates whatever name of db u wanna put)

    C- Create operations add new documents to a collection. MongoDB provides the following methods to insert documents into a collection:

    db.collection.insertOne(here data is put in JS format) 

    db.collection.insertMany()

    • R- Read operations retrieve documents from a collection .

    MongoDB provides the following methods to read documents from a collection:

    U- Update operations- Update operations modify existing documents in a collection. MongoDB provides the following methods to update documents of a collection:

    db.collection.updateOne() 

    db.collection.updateMany() 

    db.collection.replaceOne() 

    D- Delete operations- Delete operations remove documents from a collection. MongoDB provides the following methods to delete documents of a collection:

    NOTE: To move out of Mongod server we need to click ctrl + c.

    Please understand yourself the basics of CRUD and u can also go through the below provided link. https://docs.mongodb.com/manual/crud/.

    MongoDB with Node.js

    • We need native mongoDB drivers or Mongoose to interact MongoDB with Node.js.
    • Below path gives u the working process with Native MongoDB driver.
    • Please be noted that Mongoose is advantageous than native MongoDB driver to interact with Node.js, I prefer to learn Mongoose rather than any drivers.

    Create the package.json file

    [edit]

    First, create a directory where your application will live.

    mkdir myproject

    cd myproject

    Enter the following command and answer the questions to create the initial structure for your new project:

    npm init

    Next, install the driver dependency.

    npm install mongodb --save

    You should see NPM download a lot of files. Once it’s done you’ll find all the downloaded packages under the node_modules directory.

    Start a MongoDB Server

    [edit]

    For complete MongoDB installation instructions, see the manual.

    1. Download the right MongoDB version from MongoDB
    2. Create a database directory (in this case under /data).
    3. Install and start a mongod process.

    mongod --dbpath=/data

    You should see the mongod process start up and print some status information.

    Connect to MongoDB

    [edit]

    Create a new app.js file and add the following code to try out some basic CRUD operations using the MongoDB driver.

    Add code to connect to the server and the database myproject:

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    // Database Name

    const dbName = 'myproject';

    // Create a new MongoClient

    const client = new MongoClient(url);

    // Use connect method to connect to the Server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected successfully to server");

    const db = client.db(dbName);

    client.close();

    });

    Run your app from the command line with:

    node app.js

    The application should print Connected successfully to server to the console.

    Insert a Document

    [edit]

    Add to app.js the following function which uses the insertMany method to add three documents to the documents collection.

    const insertDocuments = function(db, callback) {

    // Get the documents collection

    const collection = db.collection('documents');

    // Insert some documents

    collection.insertMany([

    {a : 1}, {a : 2}, {a : 3}

    ], function(err, result) {

    assert.equal(err, null);

    assert.equal(3, result.result.n);

    assert.equal(3, result.ops.length);

    console.log("Inserted 3 documents into the collection");

    callback(result);

    });

    }

    The insert command returns an object with the following fields:

    • result Contains the result document from MongoDB
    • ops Contains the documents inserted with added _id fields
    • connection Contains the connection used to perform the insert

    Add the following code to call the insertDocuments function:

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    // Database Name

    const dbName = 'myproject';

    const client = new MongoClient(url, { useNewUrlParser: true });

    // Use connect method to connect to the server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected successfully to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {

    client.close();

    });

    });

    Run the updated app.js file:

    node app.js

    The operation returns the following output:

    Connected successfully to server

    Inserted 3 documents into the collection

    Find All Documents

    [edit]

    Add a query that returns all the documents.

    const findDocuments = function(db, callback) {

    // Get the documents collection

    const collection = db.collection('documents');

    // Find some documents

    collection.find({}).toArray(function(err, docs) {

    assert.equal(err, null);

    console.log("Found the following records");

    console.log(docs)

    callback(docs);

    });

    }

    This query returns all the documents in the documents collection. Add the findDocument method to the client.connect callback:

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    // Database Name

    const dbName = 'myproject';

    const client = new MongoClient(url);

    // Use connect method to connect to the server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected correctly to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {

    findDocuments(db, function() {

    client.close();

    });

    });

    });

    Find Documents with a Query Filter

    [edit]

    Add a query filter to find only documents which meet the query criteria.

    const findDocuments = function(db, callback) {

    // Get the documents collection

    const collection = db.collection('documents');

    // Find some documents

    collection.find({'a': 3}).toArray(function(err, docs) {

    assert.equal(err, null);

    console.log("Found the following records");

    console.log(docs);

    callback(docs);

    });

    }

    Only the documents which match 'a' : 3 should be returned.

    Update a document

    [edit]

    The following operation updates a document in the documents collection.

    const updateDocument = function(db, callback) {

    // Get the documents collection

    const collection = db.collection('documents');

    // Update document where a is 2, set b equal to 1

    collection.updateOne({ a : 2 }

    , { $set: { b : 1 } }, function(err, result) {

    assert.equal(err, null);

    assert.equal(1, result.result.n);

    console.log("Updated the document with the field a equal to 2");

    callback(result);

    });

    }

    The method updates the first document where the field a is equal to 2 by adding a new field b to the document set to 1. Next, update the callback function from client.connect to include the update method.

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    // Database Name

    const dbName = 'myproject';

    const client = new MongoClient(url);

    // Use connect method to connect to the server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected successfully to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {

    updateDocument(db, function() {

    client.close();

    });

    });

    });

    Remove a document

    [edit]

    Remove the document where the field a is equal to 3.

    const removeDocument = function(db, callback) {

    // Get the documents collection

    const collection = db.collection('documents');

    // Delete document where a is 3

    collection.deleteOne({ a : 3 }, function(err, result) {

    assert.equal(err, null);

    assert.equal(1, result.result.n);

    console.log("Removed the document with the field a equal to 3");

    callback(result);

    });

    }

    Add the new method to the client.connect callback function.

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    // Database Name

    const dbName = 'myproject';

    const client = new MongoClient(url);

    // Use connect method to connect to the server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected successfully to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {

    updateDocument(db, function() {

    removeDocument(db, function() {

    client.close();

    });

    });

    });

    });

    Index a Collection

    [edit]

    Indexes can improve your application’s performance. The following function creates an index on the a field in the documents collection.

    const indexCollection = function(db, callback) {

    db.collection('documents').createIndex(

    { "a": 1 },

    null,

    function(err, results) {

    console.log(results);

    callback();

    }

    );

    };

    Add the indexCollection method to your app:

    const MongoClient = require('mongodb').MongoClient;

    const assert = require('assert');

    // Connection URL

    const url = 'mongodb://localhost:27017';

    const dbName = 'myproject';

    const client = new MongoClient(url);

    // Use connect method to connect to the server

    client.connect(function(err) {

    assert.equal(null, err);

    console.log("Connected successfully to server");

    const db = client.db(dbName);

    insertDocuments(db, function() {

    indexCollection(db, function() {

    client.close();

    });

    });

    });

    MONGOOSE

    • How is Mongoose advantageous than any native driver?
    • It consumes less space/code than previously used driver. Please find below code and try understanding.
    • const mongoose = require('mongoose');
    • mongoose.connect("mongodb://localhost:27017//fruitDB", {useNewurlParser: true});
    • const fruitschema = new mongoose.schema({
    •   name: String,
    •   rating: Number,
    •   review: string
    • });
    • const Fruit = mongoose.model("Fruit", fruitschema);
    • const fruit = new Fruit({
    •   name: "apple",
    •   rating: 8,
    •   review: "an apple a day keeps doctor away"
    • });
    • fruit.save();
    • In the above code, it shows us inserting a document into a collection (here fruitDB is database); from this we can understand how Mongoose works in shorter and smarter way.
    • For inserting many elements or updating many elements etc; we can find syntax in the below link.

    https://mongoosejs.com/docs/api/model.html

    NOTE: There are still many concepts related to Node.js but the above mentioned concepts would give you good idea on what Node.js is.

    Thanks.