An Introduction to Node.js

Introduction to Node.js

Node.js is an open-source and cross-platform JavaScript runtime environment. It is a popular tool for almost any kind of project!

Node.js runs the V8 JavaScript engine, the core of Google Chrome, outside of the browser. This allows Node.js to be very performant.

A Node.js app is run in a single process, without creating a new thread for every request. Node.js provides a set of asynchronous I/O primitives in its standard library that prevent JavaScript code from blocking and generally, libraries in Node.js are written using non-blocking paradigms, making blocking behavior the exception rather than the norm.

When Node.js needs to perform an I/O operation, like reading from the network, accessing a database or the filesystem, instead of blocking the thread and wasting CPU cycles waiting, Node.js will resume the operations when the response comes back.

This allows Node.js to handle thousands of concurrent connections with a single server without introducing the burden of managing thread concurrency, which could be a significant source of bugs.

Node.js has a unique advantage because millions of frontend developers that write JavaScript for the browser are now able to write the server-side code in addition to the client-side code without the need to learn a completely different language.

In Node.js the new ECMAScript standards can be used without problems, as you don’t have to wait for all your users to update their browsers - you are in charge of deciding which ECMAScript version to use by changing the Node.js version, and you can also enable specific experimental features by running Node.js with flags.

A Vast Number of Libraries

npm with its simple structure helped the ecosystem of Node.js proliferate, and now the npm registry hosts over 1,000,000 open source packages you can freely use.

An Example Node.js Application

The most common example Hello World of Node.js is a web server:

This code first includes the Node.js http module.

Node.js has a fantastic standard library, including first-class support for networking.

The createServer() method of http creates a new HTTP server and returns it.

The server is set to listen on the specified port and hostname. When the server is ready, the callback function is called, in this case informing us that the server is running.

Whenever a new request is received, the request event is called, providing two objects: a request (an http.IncomingMessage object) and a response (an http.ServerResponse object).

Those 2 objects are essential to handle the HTTP call.

The first provides the request details. In this simple example, this is not used, but you could access the request headers and request data.

The second is used to return data to the caller.

In this case with:

res.statusCode = 200

we set the statusCode property to 200, to indicate a successful response.

We set the Content-Type header:

res.setHeader('Content-Type', 'text/plain')

and we end close the response, adding the content as an argument to end():

res.end('Hello World\n')

Node.js Frameworks and Tools

Node.js is a low-level platform. In order to make things easy and exciting for developers, thousands of libraries were built upon Node.js by the community.

Many of those established over time as popular options. Here is a non-comprehensive list of the ones worth learning:

  • AdonisJs: A full-stack framework highly focused on developer ergonomics, stability, and confidence. Adonis is one of the fastest Node.js web frameworks.
  • Express: It provides one of the most simple yet powerful ways to create a web server. Its minimalist approach, unopinionated, focused on the core features of a server, is key to its success.
  • Fastify: A web framework highly focused on providing the best developer experience with the least overhead and a powerful plugin architecture. Fastify is one of the fastest Node.js web frameworks.
  • hapi: A rich framework for building applications and services that enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
  • koa: It is built by the same team behind Express, aims to be even simpler and smaller, building on top of years of knowledge. The new project born out of the need to create incompatible changes without disrupting the existing community.
  • Loopback.io: Makes it easy to build modern applications that require complex integrations.
  • Meteor: An incredibly powerful full-stack framework, powering you with an isomorphic approach to building apps with JavaScript, sharing code on the client and the server. Once an off-the-shelf tool that provided everything, now integrates with frontend libs React, Vue, and Angular. Can be used to create mobile apps as well.
  • Micro: It provides a very lightweight server to create asynchronous HTTP microservices.
  • NestJS: A TypeScript based progressive Node.js framework for building enterprise-grade efficient, reliable and scalable server-side applications.
  • Next.js: A framework to render server-side rendered React applications.
  • Nx: It powers the Angular CLI which allows building full-stack applications using NestJS, Express, and Angular and easily share code between backends and frontends.
  • Socket.io: A real-time communication engine to build network applications.

A brief history of Node.js

Believe it or not, Node.js is only ten years old.

In comparison, JavaScript is 24 years old and the Web is 30 years old.

Ten years isn’t a very long time in tech, but Node.js seems to have been around forever.

I’ve had the pleasure to work with Node.js since the early days when it was only 2 years old, and despite the limited information in the wild, you could already feel that it was going to be a huge thing.

In this post, we draw the big picture of Node.js in its history, to put things in perspective.

A little bit of history

JavaScript is a programming language that was created at Netscape as a scripting tool to manipulate web pages inside their browser, Netscape Navigator.

Part of the business model of Netscape was to sell Web Servers, which included an environment called Netscape LiveWire that could create dynamic pages using server-side JavaScript. Unfortunately, Netscape LiveWire wasn’t very successful and server-side JavaScript wasn’t popularized until recently, by the introduction of Node.js.

One key factor that led to the rise of Node.js was the timing. Just a few years earlier, JavaScript had started to be considered as a more serious language, thanks to “Web 2.0” applications (such as Flickr, Gmail, etc.) that showed the world what a modern experience on the web could be like.

JavaScript engines also became considerably better as many browsers competed to offer users the best performance. Development teams behind major browsers worked hard to offer better support for JavaScript and find ways to make JavaScript run faster. The engine that Node.js uses under the hood, V8 (also known as Chrome V8 for being the open-source JavaScript engine of The Chromium Project), improved significantly due to this competition.

Node.js happened to be built in the right place and right time, but luck isn’t the only reason why it is popular today. It introduces a lot of innovative thinking and approaches for JavaScript server-side development that has already helped many developers.

2009

  • Node.js is born
  • The first form of npm is created

2010

2011

  • npm hits version 1.0
  • Larger companies start adopting Node.js: LinkedIn, Uber, etc.
  • hapi is born

2012

  • Adoption continues very rapidly

2013

  • First big blogging platform using Node.js: Ghost
  • Koa is born

2014

  • The Big Fork: io.js is a major fork of Node.js, with the goal of introducing ES6 support and moving faster

2015

  • The Node.js Foundation is born
  • IO.js is merged back into Node.js
  • npm introduces private modules
  • Node.js 4 (versions 1, 2 and 3 never previously released)

2016

2017

  • npm focuses more on security
  • Node.js 8
  • HTTP/2
  • V8 introduces Node.js in its testing suite, officially making Node.js a target for the JS engine, in addition to Chrome
  • 3 billion npm downloads every week

2018


How to install Node.js

Node.js can be installed in different ways. This post highlights the most common and convenient ones.

Official packages for all the major platforms are available at https://nodejs.org/en/download/.

One very convenient way to install Node.js is through a package manager. In this case, every operating system has its own.

On macOS, Homebrew is the de-facto standard, and - once installed - allows to install Node.js very easily, by running this command in the CLI:

brew install node

Other package managers for Linux and Windows are listed in https://nodejs.org/en/download/package-manager/

nvm is a popular way to run Node.js. It allows you to easily switch the Node.js version, and install new versions to try and easily rollback if something breaks, for example.

It is also very useful to test your code with old Node.js versions.

See https://github.com/creationix/nvm for more information about this option.

My suggestion is to use the official installer if you are just starting out and you don’t use Homebrew already, otherwise, Homebrew is my favorite solution.

In any case, when Node.js is installed you’ll have access to the node executable program in the command line.


How much JavaScript do you need to know to use Node.js?

As a beginner, it’s hard to get to a point where you are confident enough in your programming abilities.

While learning to code, you might also be confused at where does JavaScript end, and where Node.js begins, and vice versa.

I would recommend you to have a good grasp of the main JavaScript concepts before diving into Node.js:

  • Lexical Structure
  • Expressions
  • Types
  • Variables
  • Functions
  • this
  • Arrow Functions
  • Loops
  • Scopes
  • Arrays
  • Template Literals
  • Semicolons
  • Strict Mode
  • ECMAScript 6, 2016, 2017

With those concepts in mind, you are well on your road to become a proficient JavaScript developer, in both the browser and in Node.js.

The following concepts are also key to understand asynchronous programming, which is one fundamental part of Node.js:

  • Asynchronous programming and callbacks
  • Timers
  • Promises
  • Async and Await
  • Closures
  • The Event Loop

Differences between Node.js and the Browser

Both the browser and Node.js use JavaScript as their programming language.

Building apps that run in the browser is a completely different thing than building a Node.js application.

Despite the fact that it’s always JavaScript, there are some key differences that make the experience radically different.

From the perspective of a frontend developer who extensively uses JavaScript, Node.js apps bring with them a huge advantage: the comfort of programming everything - the frontend and the backend - in a single language.

You have a huge opportunity because we know how hard it is to fully, deeply learn a programming language, and by using the same language to perform all your work on the web - both on the client and on the server, you’re in a unique position of advantage.

What changes is the ecosystem.

In the browser, most of the time what you are doing is interacting with the DOM, or other Web Platform APIs like Cookies. Those do not exist in Node.js, of course. You don’t have the document, window and all the other objects that are provided by the browser.

And in the browser, we don’t have all the nice APIs that Node.js provides through its modules, like the filesystem access functionality.

Another big difference is that in Node.js you control the environment. Unless you are building an open source application that anyone can deploy anywhere, you know which version of Node.js you will run the application on. Compared to the browser environment, where you don’t get the luxury to choose what browser your visitors will use, this is very convenient.

This means that you can write all the modern ES6-7-8-9 JavaScript that your Node.js version supports.

Since JavaScript moves so fast, but browsers can be a bit slow and users a bit slow to upgrade, sometimes on the web, you are stuck to use older JavaScript / ECMAScript releases.

You can use Babel to transform your code to be ES5-compatible before shipping it to the browser, but in Node.js, you won’t need that.

Another difference is that Node.js uses the CommonJS module system, while in the browser we are starting to see the ES Modules standard being implemented.

In practice, this means that for the time being you use require() in Node.js and import in the browser.


Node.js Tutorial for Beginners: Learn Node in 1 Hour

TABLE OF CONTENT:
00:00 What is Node
03:01 Node Architecture
06:04 How Node Works
10:29 Installing Node
13:01 Your First Node Program
15:22 Node Module System
15:52 Global Object
19:14 Modules
22:51 Creating a Module
27:35 Loading a Module
32:59 Module Wrapper Function
39:53 Path Module
44:03 OS Module
48:22 File System Module
53:14 Events Module
59:33 Event Arguments
01:02:43 Extending EventEmitter
01:10:46 HTTP Module


Learn Node.js - Full Tutorial for Beginners

Node.js is an open-source, cross-platform JavaScript run-time environment that executes JavaScript code outside of a browser.

Learn all about Node.js in the full course for beginners.

⭐️Course Contents ⭐️
⌨️ (0:00:00) Installing Nodejs
⌨️ (0:05:22) Working With Modules
⌨️ (0:14:40) The Events Module and EventEmitter Class
⌨️ (0:22:32) Working With The ReadLine Module.
⌨️ (0:34:36) Working With File System Module (Creating,Reading,Deleting,Renaming) Files
⌨️ (0:45:04) Working With File System Module. Creating and Deleting Folders
⌨️ (0:57:36) Working with Readable and Writable Streams
⌨️ (1:02:40) Why you should use Streams
⌨️ (1:05:41) Pipes and Pipe Chaining. (Readable,Writable and Transform Streams)
⌨️ (1:12:36) Creating a Http Server using the Http Module
⌨️ (1:17:52) Serving Static Files with Http and File System Module (html,json,image)
⌨️ (1:24:30) Create our Package.json using Npm Init
⌨️ (1:27:18) Installing Packages using Npm (Node Package Manager)
⌨️ (1:32:23) Semantic Versioning
⌨️ (1:36:42) Getting started with Express Web Framework
⌨️ (1:40:48) Working with Express Http Get Request, Route Params and Query Strings
⌨️ (1:49:52) Serving Static Files with Express
⌨️ (2:54:36) Http Post Request with Express and Body Parser Module
⌨️ (2:00:17) Working with JSON Data with Express and the Body Parser Module
⌨️ (2:07:40) User Input Validation With Express And JOI
⌨️ (2:15:24) User Input Validation with JOI Validating Nested Object and Arrays
⌨️ (2:22:34) Getting Started With EJS Templates With Express
⌨️ (2:35:22) How does MiddleWare Work and Creating Custom Middleware
⌨️ (2:42:49) Working With The Express Router


Node JS Full Course - Learn Node.js in 7 Hours | Node.js Tutorial for Beginners | Edureka

This Edureka Node.js Full Course video will help you in learn Node.js along with practical demonstration. This Node.js Tutorial for Beginners is ideal for both beginners as well as professionals who want to master the most prominently used javascript backend framework. Below are the topics covered in this node.js tutorial video:
2:32 What is Node.js?
3:22 Client-Server Architecture
4:12 Multi-Threaded Model
6:13 Single-Threaded Model
7:43 Multi-Threaded vs Event-Driven
9:45 Uber Old Architecture
11:10 Uber New Architecture
12:30 What is Node.js?
13:05 Sucess Stories
14:20 Node.js Trend
14:40 Node.js Features
16:25 Node.js Installation
16:50 Node.js First Example
17:30 Blocking vs Non-blocking
18:50 Demo
23:50 Node.js Modules
23:50 NPM
25:10 Global Objects
26:55 File System
30:30 Callbacks
31:45 Event
33:05 HTTP
34:50 Hands On
1:09:45 Node.js Tutorial
1:10:45 What is Node.js?
1:12:10 Features of Node.js
1:13:00 Node.js Architecture
1:14:55 NPM(Node Package Manager)
1:16:20 Node.js Modules
1:16:30 Node.js Modules Types
1:16:35 Core Modules
1:16:55 Local Modules
1:17:10 3rd Party Modules
1:18:35 JSON File
1:23:30 Data Types
1:25:35 Variables
1:26:40 Operators
1:27:45 Functions
1:29:10 Objects
1:29:55 File Systems
1:33:50 Events
1:34:20 HTTP Module
1:40:02 Events
1:44:37 HTTP Module
1:45:27 Creating a Web Server using Node.js
1:45:42 Express.js
1:46:57 Demo
1:58:37 Node.js NPM Tutorial
1:59:37 What is NPM?
2:03:12 Main Functions of NPM
2:04:27 Need For NPM
2:08:07 NPM Packages
2:17:42 NPM Installation
2:18:12 JSON File
2:31:32 Node.js Express Tutorial
2:32:02 Introduction to Express.js
2:32:32 Features of Express.js
2:35:27 Getting Started with Express.js
2:39:42 Routing Methods
2:44:57 Hands-On
2:48:12 Building RESTful API with Node.js
2:48:27 What is REST API?
2:49:42 Features of REST API
2:51:12 Principles of REST API
2:56:37 Methods of REST API
2:59:52 Building REST API with Node.js
3:24:07 Node.js MySQL Tutorial
3:24:32 What is MySQL?
3:25:13 Advantages of Using MySQL with Node.js
3:27:38 MySQL Installation
3:44:23 Node.js MongoDB Tutorial
3:44:58 What is NoSQL?
3:47:53 NoSQL Databases
3:48:38 Introduction to MongoDB
3:52:48 Features of MongoDB
3:53:03 MongoDB Installation
4:36:08 Node.js Docker Tutorial
4:36:38 What is Docker?
4:39:13 Docker Working
4:41:43 Docker Basics
4:41:48 DockerFile
4:42:03 Docker Images
4:42:23 Docker Container
4:44:38 Why use Node.js with Docker?
4:45:18 Demo: Node.js with Docker
4:58:38 MEAN Stack Application Tutorial
4:59:18 What is MEAN Application?
4:59:53 MongoDB
5:00:28 Express
5:01:13 Angular
5:01:23 Node.js
5:02:17 RESTful API
5:03:02 Contact List MEAN App
6:17:57 Node.js Interview Questions


Node.js Crash Course

In this crash course we will explore Node.js fundamentals including modules such as path, url, fs, events and we will create an HTTP server from scratch without Express and deploy to Heroku.


What is Node js? | Why it is so Famous?

Node Js is the runtime environment for JavaScript.
In this video will talk about What is Node JS and Why it is so famous?
Will touch Javascript, Express, Angular, React, MEAN, MERN, MongoDB.

#node-js #javascript #web-development #angular

An Introduction to Node.js
7.90 GEEK