The server-side
JavaScript
Siddharth & Swarna
What to expect ahead?
➔ Introduction
➔ Examples
➔ Event Loops
➔ What can you do with [Link]?
➔ [Link] Ecosystem
➔ When to use [Link]?
➔ When to not use [Link]?
➔ Resources to get started with [Link]
Background
➔ V8 is an open-source JavaScript engine developed by Google for Chromium based web
browsers and is written in C++.
➔ [Link] runs on V8.
➔ It was created by Ryan Dahl in 2009.
➔ Its current version is 15.4.0 and LTS is 14.15.1.
➔ It is open-source and platform independent.
Introduction
➔ In simple words [Link] is ‘server-side JavaScript’.
➔ In not-so-simple words, [Link] is a high-performance network applications framework, well
optimized for high concurrent environments.
➔ It’s a command line tool.
➔ In [Link], ‘js’ doesn’t mean that its solely written in JavaScript. It comprises of 40% JavaScript
and 60% C++.
Introduction - continued...
➔ [Link] uses an event-driven, non-blocking I/O model, which makes it lightweight.
➔ It makes use of event-loops via JavaScript’s callback functionality to implement the non-
blocking I/O.
➔ Programs for [Link] are written in JavaScript but not in the same JavaScript we are use to.
There is no DOM implementation provided by [Link], i.e. you can not do this:
var element = [Link](‘elementId’);
➔ Everything inside [Link] runs in a single-thread.
Getting started & hello world
➔ Install the latest LTS version of [Link] from [Link]
➔ Open an editor of your choice and start typing JavaScript.
➔ Save the file with a ‘.js’ extension.
➔ Run it by using the following command
node [Link]
Example 1 - Hello
world
Following code outputs Hello
followed by world after 3s.
Event loops
➔ The event loop is what allows [Link] to perform non-blocking I/O operations
➔ Event-loops are the core of event-driven programming, almost all the UI programs use event-
loops to track the user event, for example: Clicks, Ajax
Client Clients send HTTP requests to
[Link] server.
JavaScript C++
Event loop returns
result to client. Node standard library
Event loop
(main thread)
Node bindings
(socket, http, etc)
Thread Event
V8 pool loop DNS Crypto
(libeio) (libev) (c-ares) (OpenSSL)
Response is sent back
to the event loop via C++ Threadpool
(worker threads)
Long running jobs run on
callback. worker threads.
Blocking I/O
Non-Blocking I/O
What can you do with [Link]?
➔ Package management through npm, bower, jspm, etc.
➔ Development tooling (module management with webpack, task running and automation through
grunt or gulp, linters like eslint or jslint, etc)
➔ Creation of back-end web applications.
➔ Command line tools like rimraf.
➔ Desktop applications.
➔ Building neural networks, or chat bots
Example 2 - HTTP
Server
Following code creates an HTTP
Server and prints ‘Hello World’
on the browser.
Example 3 - TCP
Server
Here is an example of a simple
TCP server which listens on
port 3000 and echoes whatever
you send it.
[Link] Ecosystem
➔ [Link] heavily relies on modules, in previous examples require keyword loaded the http & net
modules.
➔ Creating a module is easy, just put your JavaScript code in a separate js file and include it in
your code by using keyword require, like:
➔ Libraries in [Link] are called packages and they can be installed by typing
➔ NPM (Node Package Manager) comes bundled with [Link] installation.
Example 4 - Connect to MongoDB
Install MongoDB using npm, the official MongoDB driver for [Link].
Example 5 - Twitter streaming
When to use [Link]?
➔ [Link] is good for creating streaming based real- time services, web chat applications, static file
servers etc.
➔ If you need high level concurrency and not worried about CPU-cycles.
➔ If you are great at writing JavaScript code because then you can use the same language at both
the places: server-side and client-side.
When not to use [Link]?
➔ When you are doing heavy and CPU intensive calculations on server side, because event-loops
are CPU hungry.
➔ Node’s relational database support tools are not up to the expected level when compared to other
languages.
Resources to get started
➔ Visit [Link] for more information about [Link]
➔ For more on MongoDB: [Link]
Some good modules
➔ Express – to make things simpler e.g. syntax, DB connections.
➔ Jade – HTML template system
➔ [Link] – to create real-time apps
➔ Nodemon – to monitor [Link] and push change automatically
➔ Typescript – for strictly typed and runtime-error free JavaScript development
Thank you