Node.js

Álvaro José Agámez Licha

Software Developer en PSL

https://github.com/CodeMaxter

@codemaxter

Open Source Server Enviroment For Your JavaScript Apps

What is Node.js

Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine, or Microsoft ChakraCore.

 

  • ​Is an open source server environment.
  • Runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.).
  • Uses a lightweight and efficient event-driven, non-blocking I/O model.

Node.js is a single threaded language which in background uses multiple threads to execute asynchronous code.

Node.js is fast and efficient because use a single thread:

 

  • Thread-based networking is relatively inefficient,
  • Thread-base is very difficult to use.
  • Users of Node are free from worries of dead-locking the process, since there are no locks.

Node = Not Single Threaded
Event Loop = Single Threaded

 

  • Multiple Threads run in parallel and this is true of Node.
  • The event loop processes events in a single thread concurrently.

 

Everything in JavaScript runs parallel except your code. Your code executes one thing at a time even when other workers are doing their jobs concurrently.

Installation

Many will recommend that you head to the official Node download page and grab the Node binaries for your system. While that works, I would suggest that you use a version manager instead.

 

For this tutorial, we will use nvm.

Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world, but Node.js also has a great API, so please, before searching for something in the NPM repository or on the Internet, search the API.

The Package Manager

Open your terminal and type the following:

Installing a Package Globally

npm install -g jshint

This will install the jshint package globally on your system. We can use it to lint a es6.js file:

jshint es6.js

We can also install packages locally to a project, as opposed to globally on our system. Create a test folder and open a terminal in that directory. Next type:

Installing a Package Locally

npm init -y

This will create and auto-populate a package.json file in the same folder. Next, use npm to install the lodash package and save it as a project dependency:

npm install lodash --save

Node.js Introduction

By Alvaro Agamez

Node.js Introduction

  • 513