NodeJS From Scratch
Published: 2023-04-05 09:36:48

## Session - 1 
1. JS engine 
2. How node comes into the picture -How Js works outside browser?
3. Why use Node JS?
4. Npm (what and why)?
5. npm init // npm install express
6. Package.json( Why and What)? 
     1. version (1.0.0)
     2. scripts
7. Let's create first script
8. Create second script with nodemon
9. package.json VS package-lock.json // command : npm ci 
10. What is Server ?
11. What is HTTP ? and HTTP Server ??
12. Create an HTTP Server
    1. require to import a module // require()
    2. createServer() method
    3. server.listen();
13. Calling the server endpoint from Postman for Log Server Date- 
    Time
14. The Response Object
    1.  res.end();
    2.  res.write(); //Sending Data with Response
   3.   res.writehead(); //Specifying Headers in Response
15. Request Object
    1. req.url - contains the url of the request
16. Activity - Extending APIs
17. Activity - Advanced Extending APIs

## Session 2 
1. Whats is express ??
2. Using Express to create http server and send data
   1. create an app instance
   2. app.listen()
   3. app.get() //adding route
   4. res.end() , res.json , res.write(), res.send() 
      , res.senStatus() //response
3. express request object
   1. req.params()
   2. req.query()
4. Filter all curency by min_Value -- use req.queryParam,
5. What is controller?
    1. create controller?
    2. Need of controllers?
    3. flow of controllers
6. Import/export in Node JS?
   1. require();
   2. module.exports {}; 
7. In Express, the routes are served in the first come first 
   server manner. ex user search by gender and age route position 
   changed then its work well.

## Session 3
1. Express Routes
   1. what is express router?
   2. Why use??
   3. How to Manage API versioning?? //ans: router
   4. Creating a router
   5. new Router instance 
      //const router = require("express").Router();
   6. router.get("/");
   7. Use the routes
   8. app.use("/path", xyzroutes);
   Activity == club currency Routes.
2. Activity - Club User Routes
3. Validating Routes
   1. if else if else check to validate
   2. Validations using Joi
      1. Creating a Joi Schema
      2. Create a schema with the functions in the Joi object
      3. Adding Schema Keys
      4. Create a validator function
      5. Using the validator function 
      //currencies  
4. Environment Variables
   1. How to use environment variables?
   2. .env file ->  // process.env.ROUTE_PASSWORD   
   3. require('dotenv').config() // npm install dotenv

## Session 4
1. what is middleware
2. Why middleware
3. middleware Architecture
4. flow of excution  middleware
//create middleware for verify auth
//create middleware for validateSearchQuery

## mongoDB Database
1. How to Storing Stuff in DB
2. SQL vs MongoDB
3. Intro to MongoDB
   1. Data is stored in collections and documents
   2. Documents in MongoDB
   3. Format of Data in MongoDB //BSON
4. Using Mongo and storing documents
   1. Use the database test  
      > use test
   2. Check to see if it has any collections inside it? 
      > db.getCollectionNames() 
   3. Add a new collection named “blogs” if already doesn’t exist
      > db.createCollection("blogs")
   4. Add a new collection named “blogs” if already doesn’t exist
      > db.createCollection("blogs")
   5. Add some documents to this collection
      > db.blogs.insert({publishedAt: null, content: "", author: 
        []})
   6. List down the documents of the blogs collection
      > db.blogs.find({}).pretty()
 
5. Storing Data in MongoDB with NodeJS
   1. what is Object Data Modeling library ODM ??
   2. What and Why Mongoose  ??
   3. How to use mongoose ??
      1. npm i mongoose
      2. Connecting to DB
         const DB_URI = "mongodb://127.0.0.1:27017";
      3. mongoose.connect(`${DB_URI}`)
      4. Set specifying  DB
6. Mongoose Schema
   1. Creating a schema
7. Mongoose Model
   mongoose.model("xyz", xyzSchema);


# Session 5

1. <u>Create - Writing to DB</u>
   1. create a new Blogs instance
   2. const newBlogDoc = new Blogs({title: "First Blog"});
   3. router.get("/new", createNewBlog);
   4. app.use("/blogs", blogRoutes)
   5. update our schema by passing in options object for keys
   6. Confirm unique title 
   7. Delete the current blogs collection from the mongodb
      > db.blogs.drop()
   8. POST- update title dynamically
   9. router.post()
   10. Parsing body from POST request -> 
      in----> index.js --> app.use(express.json());
   11. Confirming the document in the DB
      > db 
      test 
      > db.getCollectionNames(); 
      [ ]
   12. Saving the object in the DB -- by use  .save()

2.<u>Reading from the DB</u>
    1. create getAllBlogs in controller -> 
    2. Update the routes to support the GET / route that is 
       controlled by getAllBlogs 
       router.get()
   
3.<u>Delete - Removing documents</u>
    1. check and remove from from DB command
       > db.blogs.find({}).pretty()
       > db.blogs.remove( 
         { _id: ObjectId("61c81cca4112d97895e4911b") } );
    2. need to be able to do this using a network call
       1. router.delete("/:id", deleteBlogWithId);
       2.  controller -- -------> deleteBlogWithId --------
           ------> findOneAndDelete({ _id: id });
      
4.<u>Updating content</u>
   1. findOneAndUpdate(filter, update, { new: true });
   2. const filter = { _id: id }; //conditions to find the 
      document
   3. const update = req.body; //updates to be performed
   4. router.patch("/:id", updateBlogsWithId);

5.Timestamps in Schema
   { timestamps: true } 
   //will add createdAt and updatedAt timestamps
 
## Session 6
1. Nested Schema
   1. const authorSchema = new mongoose.Schema({ });
   2. { _id: false }
   3. Fixing the validation for emails
      1. npm i validator

2. Building Queries in MongoDB 
   1. reate a route and a controller 
      1. searchBlogs()
      2. A special operator $elemMatch
      3. $or operator
      4. RegEx
      5.  findOne() vs findById()

3. Business Logic Layer - Services
   1. what is Services?? Why ??? application ?? and flow??
   2. Now get rid of business logic from controllers
       Step 1 - Create a new service file
       Step 2 - Transport logic from controller to service
   3. Activity - Create New Services //Apply oops encapsulate
      1. Creating Blog Service Class
      2. Activity - Use classes to create a new blog
      3. Note on naming conventions

4. Final Architecture of our express app.    
![Flow of backend in node_JS MVC ](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3mo7fcgvdduarght17xn.jpg)