Skip to content

Node.js is a platform built on the Chrome JavaScript runtime that can help you build fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient—perfect for data-intensive real-time applications that run across distributed devices

Notifications You must be signed in to change notification settings

ArchitectAK/Learning-Node.js

Repository files navigation

Learning-Node.js

1. Introduction to Node

  • Understanding Node

    • As an asynchronous event-driven JavaScript runtime, Node.js is designed to build scalable network applications. Upon each connection, the callback is fired, but if there is no work to be done, Node.js will sleep. Furthermore, users of Node.js are free from worries of dead-locking the process, since there are no locks. Almost no function in Node.js directly performs I/O, so the process never blocks
    • More about node is here.
  • Download and install node

    • download and install latest version for your OS from here
    • check if node is installed by running command node -v on your terminal
    • above command will print veriosn of node if node istalled.
    % node -v
    v10.21.0
    
  • Why is Javascript is Node's language?

    • Syntax is same for frontend and backend
    • Shared code and data structures between frontend and backend
      • shared libraries such as underscore, encryption lib
      • custom algorithm and logic for funtions
      • data structure and object definitions
  • The benefits and features of Javascript

    • Frontend and backend share the language
    • Frontend and backend share the code
    • Dynamic language - this means type is determined by the value , not when variabe is declared
    • JSON comes naturally with Javascript
  • Asynchrounous tasks and callbacks

    • When you execute a task synchronously, you wait for it to finish before moving on to another task which is what causes blocking.
    • On the other hand, when you execute tasks asynchronously, you can move on to another task without having to wait for the previous task to finish
    • The most common way to get started with asynchronous code is to do callbacks. So let's take a look at some code.

2. Understanding NPM: Node Package Manager

  • Write your own module - You can create your own modules, and easily include them in your applications. let see an easy example that creates a module that returns a date and time object
  • Manage third-party package with npm -- install any third party library and use it your code. I have created a sample code file which uses lodash library using npm.
  • What is package.json file?
    • What if we end up installing several third party packages, and we want to keep track of what we have installed and are dependent on in a list somewhere? That's where the "package.json" file comes in.
    • run npm init command to initiate npm in project repo.

3. Reading and Writing files

  • Reading from files - reading file from file system sample is here.
  • Access directories - reading directories from file system sample is here.
  • Write to a file - writing file to file system sample is here.

4. Exploring Web framework

  • Node.js framwework -
    • What is a framework? It's sometimes interesting when we look at a definition in its generic form. For example, a framework is an essential supporting structure of a building, vehicle, or object.
    • And in software, it's essentially the same thing. It is a supporting structure that allows you to build on top of it. When it comes to web, and we want to build large APIs, or maybe HTTP servers, we can leverage web frameworks, and there are several options we can look at.
    • Each of these provide us with the structure and components you need to quickly make serving static files, like traditional websites, easy. Or we can put together a web API to interact in a web app. A web API is a service that allows us to get and save data to our server or back end, such as a web API that allows us to maybe create users, serve a list of users, etc.
  • Express and Socket.io - Socket.io enables real time, bidirectional, event based communication. Express on the other hand allows the client to send a request to the server, but the server cannot send request to the client and so it does not have bidirectional communication. Socket.io solves this. In other words, we can push notifications from the server to the client when an event happens, as well as other data

5. Demo Chat App Client for the browser

  • Static serving with Express

    • You can either host static content or dynamic content.
    • Static content can be things like HTML files for web sites or images, videos, etc.
    • Dynamic content, on the other hand, is served through a web API, or sometimes it's used to serve dynamic webpages where the content or the view is composed on the server itself.
  • Creating browser app

  • Create a get messages service

  • Create a post messages service

  • Connect to Socket.io from the browser app

    • With Socket.IO, we will be able to notify clients or the app running in the browser when another user has sent a chat message, that way the app will update their message list instantly.
    • Socket.IO works as pushing notification work.
    • The nice thing about the Socket.IO library is that if a browser is older and does not support WebSockets, it will default back to polling and it has that functionality built in so it will always work, even if it's not as efficient with older browsers.
  • Create your Socket.io event

  • Sample Code above steps is here

6. Exploring Databases

  • Types of data frameworks
    • There are many different database technologies, such as MongoDB, MySQL, Veras, and many more. Node has packages to work with most of these options.
    • Most of these options fall into two categories: Either SQL or NoSQL.
  • SQL stands for structured query language.
    • The key word here is structured, meaning the data stored inside is structured.
    • The database knows and cares about the structure, and the tables that store the data must be designed with that in mind.
    • The object classes, which represent the items that will go into those structured tables, must also be designed. There is a lot of up-front design work in doing so, but also some performance and query benefits in some cases.
  • NoSQL, on the other hand, is the absence of that structure.
    • We just put data into collections without paying much attention to the structure. As a result, there's less up-front design work, and possible performance increases in some cases.
  • There's no good solution for all cases, and each project has requirements that might match SQL or NoSQL better or worse.
  • A good analogy from stack overflow is thinking of SQL like an automatic car, where a lot of the manual work is done for you, and some performance improvements, in other cases. And NoSQL is like a manual, or standard car.
  • Something to keep in mind is that there are some databases that share the features of SQL and NoSQL.
  • Set up MongoDB
  • Mongoose
    • Mongoose allows us to elegantly work with our mongodb database with object schemas.
    • Object schemas are JavaScript objects we create that will represent the type of data we will be putting in our database.
    • Even though mongodb is NoSQL, and does not have structure, with Mongoose, there is a bit of structure we must design initially.
    • This gives us some benefits of easily interacting with our data through objects, and other things such as validation.

About

Node.js is a platform built on the Chrome JavaScript runtime that can help you build fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient—perfect for data-intensive real-time applications that run across distributed devices

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published