Code Monkey home page Code Monkey logo

learning-nodejs's Introduction

Learning Node

This is the repo i created and updated while learning Node. -Shrikant Kalar

Notes

  1. Runtime Env for running js code outside browser.

  2. Node apps are Single-Threaded.

  3. Ideal for Disk, Network access intensive app & Unideal for CPU intensive app. e.g. video encoding, img manipulation service

  4. Asynchronous (Non-Blocking): A Single thread handles all requests. uses Event Queue

  5. Synchronous (Blocking): A thread per request is used.

  6. Similar to window Obj in browser we have global Obj in node, but variables are not attached to this global obj as they are attached to window in browser, they are only scoped within the file.

  7. Modules: Every file in node is a module. require() is used to import modules. module.exports is used to export modules. By default es6 modules are not supported in node.

  8. Node wraps every file in a Module Wrapper Function which is an IIFE (Immediately Invoked Function Expression) to create a scope for the file.

  9. File System module has Sync & Async methods. Always use Async methods as they don't block the thread.

  10. Events is a signal that something has happened in our app. EventEmitter is a class that emits events. on() is used to listen to events. emit() is used to emit events.

  11. To raise an event we need to create a class that extends EventEmitter class and then emit the event.

  12. Semantic versioning(SemVer): MAJOR.MINOR.PATCH e.g. 3.2.1 ^ - same as 3.x.x (same major version) ~ - same as 3.2.x (same major & minor version)

  13. REST: Representational State Transfer. Convention for building http services. CRUD operations.

  14. Route Parameters: required values for backend. e.g. /api/course/1

  15. Query String Params: optional data for backend. e.g. /api/courses/2023?sortBy=name

  16. 404 - Not Found status code. Send when a requested resource doesn't exist on server.

  17. When we POST, PUT, DELETE an object on server, we should return that object in the body of the response (with id) and perform input validation for post & put.

  18. Input Validation: Always Validate input on server side as well as on client side. For js,ts use joi, zod.

  19. Middleware: A fn that takes a req object and either returns a res to the client or passes control to another middleware fn. e.g. express.json()

  20. Configuration: Handle configuration settings for diff envs using config package.

  21. Debugging: Use debug package. Shorter, more Organized & more Controllable via name spaces.

  22. Templating Engines: Used to send html along with data. pug, mustache, ejs pkgs.

  23. For every api endpoint we should have a separate file/module.

  24. Asynchronous is differnt from Concurrent or Multi-threaded. We only have a single thread in node.

  25. Async Patterns - Callbacks, Promises, Async/Await

  26. Promise - holds the eventual result of an async operation. pending, resolved/fulfilled or rejected/error states.

  27. Change Async fn that takes a callback, to return a Promise.

  28. When Rejecting a promise, always pass an Error object.

  29. Promise.all() - run multiple promises in parallel. not waiting for one to finish before starting another.

  30. Promise.race() - when we want to do something as soon as one of the promises in the array resolves.

  31. Async/Await - syntactic sugar over promises. Await can only be used inside an Async fn. Try, Catch block.

  32. Collection in mongodb is similar to Table, Document is similar to Row in relational db.

  33. Mongoose - ODM (Object Document Mapper) for mongodb. Schema defines the shape of documents in a collection. Model is a class made from Schema, used to create documents.

  34. Comparison Operators - eq, ne, gt, gte, lt, lte, in, nin

  35. Logical Operators - or, and, nor, not

  36. REGEX - /pattern/flags

  37. Updating - Query First(findbyId), Update First(findbyIdAndUpdate,updateOne,updateMany)

  38. Deleting - deleteOne, deleteMany, findByIdAndDelete

  39. DB level Validation is not possible in mongoDB. Use Mongoose for validation.

  40. Built-in Validators - required, minlength, maxlength, match, enum,min,max.

  41. No real Relationships like RDBs in mongoDB.

  42. Relationships in MongoDB - Referencing (Normalization), Embedded Documents (Denormalization), Hybrid (Snapshot of data at a point in time).

  43. Referencing - CONSISTENCY, but slower. Embedding - PERFORMANCE, but inconsistent.

  44. HYBRID - Embed needed properties of a document & also add ref to that document.

  45. We need to think about the Queries we will be running ahead of time and design DB based on those queries.

  46. Embedded Documents - most features available in Normal Documents are also available in Embedded Documents. BUT they can't be saved on their own.

  47. Transactions - Ensures all operations succeed or none of them are applied to DB. They all succeed or rollback. They are Atomic.

  48. ObjectID - 12 bytes, 1st 4 bytes - timestamp, next 3 bytes - machine identifier, next 2 bytes - process identifier, next 3 bytes - counter.

  49. mongoose.isObjectIdOrHexString() - check if a string is a valid ObjectID.

  50. Joi-ObjectId - custom joi validator for MongoDB ObjectID.

  51. Authentication - process of determining whether a user is who they claim to be.

  52. Authorization - process of determining if a user has permission to perform a given operation.

  53. joi-password-complexity - custom joi validator for password complexity.

  54. Hashing - process of converting input of any size into a fixed-size string. Used in hash tables for fast data retrieval & in cryptography for storing passwords.

  55. Salting - adding random string to the password before hashing, making tha hash output unique. Used in conjunction with hashing to make it more secure.

  56. bcrypt - use for hashing & salting passwords.

  57. JWT - JSON Web Token. Long str identifying a user. Used for securely transmitting information between parties as a JSON object. Signed using a secret or public/private key pair. Has 3 parts - Header, Payload, Signature.

  1. Information Expert Principle - an object that has enough info and is an expert in a given area, that obj should be responsible for making decisions and performing tasks related to that area.

  2. Arrow Fns use when creating standalone fns. Regular Fns use when creating methods inside objects/class.

  3. 401 - Unauthorized status code. Send when a user tries to access a protected resource without providing a valid JWT.

  4. 403 - Forbidden status code. Send when a user tries to access a protected resource with a valid JWT but doesn't have permission to perform the operation.

  5. Express Error Middleware - register it after all the other middlewares. 1st arg is err.

  6. express-async-errors - use to handle async errors in express, instead of try catch block.

  7. Transport - a storage device for your logs. used in Winston Logger.

  8. Uncaught Exception - an exception that was not caught by a try catch block. Use process.on('uncaughtException') to handle it. Also catches Unhandled Promise Rejection.

  9. Unhandled Promise Rejection - a promise that was rejected but no catch block was used to handle it. Use process.on('unhandledRejection') to handle it.

  10. Exit the process when an uncaught exception or unhandled promise rejection occurs. process.exit(1)

  11. Log to both the Db & File because while DB is better for querying if the Db is down, we can still log to the file.

  12. Automated Testing - writing code to test our code automatically. Production code is tested by test code. Benefits - time saving, find bugs before deployment, refactoring confidence, quality code. Types - Unit, Integration, End-to-End.

  13. Unit Testing - testing individual units of an app without its external dependencies. Benefits - fast execution but less confidence. Use - Logic

  14. Integration Testing - testing the app with its external dependencies. Benefits - more confidence but slower execution.

  15. End-to-End Testing - testing the app through its UI. Benefits - most confidence but slowest execution. Drawbacks - very brittle.

  16. Number of Unit tests should be greater than or equal to the number of execution paths.

  17. Test code should always be clean and maintainable. It should same high quality as production code.

  18. Tests should neither be too specific nor too general.

  19. Use Mock fns to unit test a fn that directly or indirectly talks to an external resource.

  20. With Jest Mock fns we can easily tell if they've been called & also check the arguments they were called with.

learning-nodejs's People

Contributors

shrikantkalar023 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.