Javascript – Server Side JS

Implementation

As Wikipedia states: “Node.js is a packaged compilation of Google’s V8 JavaScript engine, the libuv platform abstraction layer, and a core library, which is itself primarily written in JavaScript.” Beyond that, it’s worth noting that Ryan Dahl, the creator of Node.js, was aiming to create real-time websites with push capability, “inspired by applications like Gmail”. In Node.js, he gave developers a tool for working in the non-blocking, event-driven I/O paradigm.After over 20 years of stateless-web based on the stateless request-response paradigm, we finally have web applications with real-time, two-way connections.

What it really means is that Node.js is not a silver-bullet new platform that will dominate the web development world. Instead, it’s a platform that fills a particular need. And understanding this is absolutely essential. You definitely don’t want to use Node.js for CPU-intensive operations; in fact, using it for heavy computation will annul nearly all of its advantages. Where Node really shines is in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability.

How it works under-the-hood is pretty interesting. Compared to traditional web-serving techniques where each connection (request) spawns a new thread, taking up system RAM and eventually maxing-out at the amount of RAM available, Node.js operates on a single-thread, using non-blocking I/O calls, allowing it to support tens of thousands of concurrent connections held in the event loop.

Package Management

To handle the packages, you will need a package manager. Most commonly used package managers include npm, yarn and pnpm.

When discussing Node.js, one thing that definitely should not be omitted is built-in support for package management using NPM, a tool that comes by default with every Node.js installation

npm

npm is the default package manager and is most widely used. It is perfect for beginners and has terrific support. The previous versions had two main issues:

  • Initially, npm didn’t have a lock file. The exact versions of each dependency weren’t precisely stated. The code would be run with the latest versions which caused compatibility problems.
  • Npm wasn’t deterministic — different versions of packages were allowed on different machines.

Yarn fixed these problems and attracted a massive audience. Later, npm updates fixed the issues and made it as usable as Yarn.

Yarn

Yarn was built by Facebook with the goal to fix scalability issues npm had at the time. Three core principles of yarn are speed, reliability, and security. Yarn can install packages faster by using local caching — it is much faster to grab a package from cache memory than to wait for the download. Local caching also enables offline mode — packages in cache can be directly installed. Yarn was first to feature a file with locked package versions so the code wouldn’t be forcibly run on the latest version. Security is ensured with the use of checksums before execution.

There are some unique features of yarn which make it faster than npm. Installation of packages is parallel, while npm installs sequentially. Yarn guarantees that dependencies will be installed the same way on a different machine, regardless of the install order. 

pnpm

If we have ten projects which use the same ten packages, each project will have its own copy in node_modules. This practice isn’t very efficient in terms of disk space. pnpm solves this problem by installing only one copy of the package on disk. The node_modules directory will only contain a hard link to the location of the package on the disk. Developers of the package manager claim it is as fast as yarn and npm — in some cases even faster. pnpm is strict in the sense that it only accesses the dependencies specified in package.json. It is great for multi-package repositories (monoreps).

Rush

Rush technically isn’t a package manager, but it allows you to pick out whether you want to use npm, yarn or pnpm. It enforces that the same version of the package manager is used by all team members.

Core Modules

Some of the most useful npm modules today are:

  • express – Express.js—or simply Express—a Sinatra-inspired web development framework for Node.js, and the de-facto standard for the majority of Node.js applications out there today.
  • hapi – a very modular and simple to use configuration-centric framework for building web and services applications
  • connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
  • socket.io and sockjs – Server-side component of the two most common websockets components out there today.
  • pug (formerly Jade) – One of the popular templating engines, inspired by HAML, a default in Express.js.
  • mongodb and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
  • redis – Redis client library.
  • lodash (underscore, lazy.js) – The JavaScript utility belt. Underscore initiated the game, but got overthrown by one of its two counterparts, mainly due to better performance and modular implementation.
  • forever – Probably the most common utility for ensuring that a given node script runs continuously. Keeps your Node.js process up in production in the face of any unexpected failures.
  • bluebird – A full featured Promises/A+ implementation with exceptionally good performance
  • moment – A JavaScript date library for parsing, validating, manipulating, and formatting dates.

CLI Commands

OptionDescription
v, –versionIt is used to print node’s version.
-h, –helpIt is used to print node command line options.
-e, –eval “script”It evaluates the following argument as JavaScript. The modules which are predefined in the REPL can also be used in script.
-p, –print “script”It is identical to -e but prints the result.
-c, –checkSyntax check the script without executing.
-i, –interactiveIt opens the REPL even if stdin does not appear to be a terminal.
-r, –require moduleIt is used to preload the specified module at startup. It follows require()’s module resolution rules. Module may be either a path to a file, or a node module name.
–no-deprecationSilence deprecation warnings.
–trace-deprecationIt is used to print stack traces for deprecations.
–throw-deprecationIt throws errors for deprecations.
–no-warningsIt silence all process warnings (including deprecations).
–trace-warningsIt prints stack traces for process warnings (including deprecations).
–trace-sync-ioIt prints a stack trace whenever synchronous i/o is detected after the first turn of the event loop.
–zero-fill-buffersAutomatically zero-fills all newly allocated buffer and slowbuffer instances.
–track-heap-objectsIt tracks heap object allocations for heap snapshots.
–prof-processIt processes V8 profiler output generated using the v8 option –prof.
–V8-optionsIt prints V8 command line options.
–tls-cipher-list=listIt specifies an alternative default tls cipher list. (requires node.js to be built with crypto support. (default))
–enable-fipsIt enables fips-compliant crypto at startup. (requires node.js to be built with ./configure –openssl-fips)
–force-fipsIt forces fips-compliant crypto on startup. (cannot be disabled from script code.) (same requirements as –enable-fips)
–icu-data-dir=fileIt specifies ICU data load path. (Overrides node_icu_data)