An introduction to Node.js application development
1. Introduction to Node.js application
development
Dr. Jayaraj Poroor
Founder & CEO, Shelloid
http://shelloid.com http://shelloid.org
GTech TFG, Jan 21 2015
2. About Shelloid
Open Source Technology (http://shelloid.org)
Operational Log Intelligence (http://shelloid.com)
3. Server-side technology requirements
● Performance
o Need optimized execution
● Concurrency
o Need to support many concurrent client requests
● Library support
o e.g., database interfacing.
● Framework support
o For rapid application development.
4. Node.JS
● Performance
o Google’s V8 VM with native JIT compilation
● Concurrency
o Asynchronous I/O (libuv) + clustering
● Library support
o 39K projects in Github
● Framework support
o e.g., Express.JS
5. Synchronous vs Asynchronous I/O
● Synchronous I/O
o Thread blocks till I/O request is complete.
o e.g., $result = mysql_query(“....”); //PHP
● Asynchronous I/O
o Thread does not block
o Uses callback mechanism to notify completion
o e.g., conn.query(“...”, function(err, rows) { }
);
6. Node.JS Threading model
1. Single computational thread
o Non-blocking, interleaved request processing
o Background worker threads for I/O processing
2. Clustering to utilize multi-core machines
3. No shared memory between cluster
processes.
7. Node.JS : When to use
● Great for
o I/O-centric applications
e.g., DB queries, File I/O , network I/O,
invocation of other web services.
o Real-time communication
WebSockets, HTTP long polling
● Not so great for
o Compute-centric applications
e.g., High-end machine learning backend
9. Anatomy of a “hello world” Node.js
var http = require('http');
var server =
http.createServer(function (req, res) {
res.writeHead(200,
{'Content-Type': 'text/html'});
res.end('<h1>Hello World</h1>');
});
server.listen(3000);
No separate HTTP
engine like Apache
required.
Command:
node app.js
10. Serving a file
Code source: https://github.com/substack/stream-handbook
Buffers the entire file in memory.
● Inefficient for large files.
● Client needs to wait till entire file
is read.
11. Node.js Streams
● Data served in chunks.
● Data event fires whenever a new
chunk is ready.
Code source: Node.js in Action
12. Streams can be piped!
File stream (input) is piped to the
response stream (output).
The response will employ chunked
Transfer-Encoding.
Code source: https://github.com/substack/stream-handbook
13. Package management
● npm (Node Package Manager)
o Provides simple & powerful package management.
o Reads module dependencies from package.json
o Typical usage: npm install or npm update.
o Can store all dependent modules locally or globally
17. Connect: usage Create a Connect ‘app’.
● Will store all middleware.
● Itself just a function.
Create a middleware ‘stack’.
Requests will execute middleware
functions till ‘next()’ is not called
or end of stack is reached.
Code source: https://www.npmjs.org/package/connect
18. Connect: usage (2)
● Middleware can be mounted
on specific URL endpoints.
● Does basic routing.
● Error handler middleware.
Create Connect-enabled server.
Code source: https://www.npmjs.org/package/connect
OR
27. Database connectivity
npm install mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret'
});
connection.connect();
connection.query('...', function(err, rows, fields) {
if (err) throw err;
connection.end();
}); https://github.com/felixge/node-mysql
npms available for virtually
any SQL/NoSQL
database!
Query completes
only when the
callback is invoked!
28. Authentication
● passport.js
o Authentication middleware for node.js.
o 140+ authentication strategies.
o Supports OpenID and OAuth
o http://passportjs.org
o Session data can be serialized into a store like
Redis.
npm install passport
29. Read more
● Node.js in Action
● http://expressjs.com
● http://www.nodebeginner.org/
30. Using Shelloid
npm install -g shelloid
shelloid app-folder [env name]
Learn more at http://shelloid.org
Requires
admin/super user
privilege
31. Dr. Jayaraj Poroor
Founder, Shelloid Systems
Open source cloud-ready Node.js application server
jayaraj@shelloid.com
http://shelloid.com
Thank You