Where it came from?
created by
Ryan Dahl in 2009
later on sponsored by
Inspired by
Ruby and Python
Built on top of Google's V8 javascript engine
What it solves?
Makes it easier to develop and run web applications quickly
const http = require('http')
const server = http.createServer((request, response) => {
response.end('Hello World!')
})
server.listen(8000)
Networking coolness not limited to http
const net = require('net')
const sockets = []
const server = net.createServer((socket) => {
sockets.push(socket)
socket.on('data', data => {
for (const target of sockets) {
if (target === socket) continue
target.write(data)
}
})
socket.on('end', () => {
const index = sockets.indexOf(socket)
sockets.splice(index, 1)
})
})
server.listen(8000)
Non-blocking I/O for high concurrency
- Based on "libuv" async event pool library
- There is no possibility of halting execution
- Reduces heap allocation cost significantly for individual connections
- Event loop exits when there is nothing left to do
Advantages
and notable success stories
Everyone in the business is already familiar with the concept
Javascript is as old as the internet
JSON is a widely known and accepted data exchange format
and since it is a first class citizen in js world,
no need for additional serialization, mapping layers
Productivity boost
doing more with less code and less hardware also means less time and less money which made everyone happy
Cross-platform
It is very easy to deploy and run your code on any platform
Package Management done right
npm founded soon after its take off and is the default standard for what it does ever since
And yet it's still competitive
Thanks to V8, it's quite efficient and performant
So it thrived!
A huge community quickly gathered around it and new ways of doing everything with javascript started flourishing from everywhere
Enterprises started jumping in
- LinkedIn launches its mobile app (2011)
- Uber re-engineers its app (2011)
- Walmart re-engineers its mobile services (2012)
- eBay goes live with its first Node.js app (2013)
- PayPal releases kraken (2013)
- Netflix re-builds its web application (2014)
- Joyent establishes Node.js Foundation with IBM, PayPal, Microsoft, Fidelity, The Linux Foundation (2015)
- Google and Intel joins Node.js Foundation (2016)
Web Frameworks
- Express
- Meteor
- Sails
- MEAN stack
- Derby
- ...
RESTful APIs
- Hapi
- Koa
server.route({
method: 'GET',
path: '/hello',
handler: function (request, h) {
return'hello world';
}
});
Websockets
- ws
- socket.io
- sockjs
// client side
var socket = io();
$('form').submit(function(){
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
// server side
io.on('connection', function(socket){
socket.on('chat message', function(msg){
console.log('message: ' + msg);
});
});
Extensive toolset for development and testing
Code linting
enforcing and maintaining code quality
ESLint has 250+ available rules
Front-end revolution
- Styling pre-processors (less, sass)
- Task automation and bundling (grunt, gulp, webpack)
- SPA frameworks with extensive cli support (angular, react, vue)
Electron Platform
Desktop GUI framework. Node.js for backend and Chromium for front-end.
Disadvantages
and Controversies
Single-threaded
which means a single node process runs only on a single core and it can't make use of any additional cpu cores
However, fortunately there are workarounds like clustering or using process managers
Hammer and nail syndrome
if all you have is a hammer, everything looks like a nail
Changing so fast
The pace for change of not only the ecosystem but also the platform might be challenging to keep up with
Lack of structure
Both in language and project levels
Runtime errors
really easy to mess up without type-safety,
requires more defensive coding, rigorous testing
Problems with the ecosysytem
packages, packages everywhere.
node_modules getting huge in size with no effort
left-pad chaos
module.exports = leftpad;
function leftpad (str, len, ch) {
str = String(str);
var i = -1;
if (!ch && ch !== 0) ch = ' ';
len = len - str.length;
while (++i < len) {
str = ch + str;
}
return str;
}
npm messing up linux systems
'sudo npm' was changing the ownership of system files, permanently breaking the operating system.
Virus in eslint-scope
Ownership of a popular package gets compromised and injected malicious code steals npm credentials from running machines
Platform forks
- IO.js (2014)
- Ayo.js (2017)
Where it goes?
Ignition
new interpereter
TurboFan
new optimizer
V8
getting stronger
deno: A secure TypeScript runtime on V8
Serverless Architecture
Progressive Web Applications
Thanks a lot for listening!
find me on github.com/koraytaylan
Node.JS Introduction
By Koray Taylan Davgana
Node.JS Introduction
- 627