3. About me
• Currently Head of Engineering @
Hopscotch
• Xoogler
• Entrepreneur
• Corporate Trainer
• Author
• AngularJS & AngularJS Up &
Running for O’Reilly
• MBA
• Geek!
4. Agenda
• 1 day
• Smaller, multiple sessions
• Code Along
• As fast or slow as needed
• Questions, interrupt any time
5. Agenda - Continued
• Introduction
• JS on the Server
• NodeJS Concepts
• Starting NodeJS
• NPM & Requires
6. Agenda - Continued
• Working with Node
packages
• Async
• Express
• Routes & Config
• NPM & Requires
7. Agenda - Continued
• Middleware
• Working with MongoDB
• Build & Deploy
• Sockets?
17. A Thinking Paradigm Shift
teams = teamsDb.getTeams();
for (var i = 0; i < teams.size(); i++) {
team = teams.get(i);
team.setExtraInfo(
teamsDb.getTeamExtraInfo(team.getId());
}
return teams;
18. How would we speed it up?
• Threading?
• Parallelization?
• Identifying the bottlenecks
19. What if?
• teamDb.getTeams was asynchronous
• teamDb.getTeams(function(teams) {});
• Same for teamDb.getTeamExtraInfo
• teamDb.getTeamExtraInf(function(team) {});
20. A Thinking Paradigm Shift
teamsDb.getTeams(function(teams) {
for (var i = 0; i < teams.size(); i++) {
team = teams.get(i);
teamsDb.getTeamExtraInfo(team.getId(),
function(teamExtra) {
team.setTeamExtraInfo(teamExtra);
});
}
// return teams; ?????
});
21. Now what about NodeJS?
• Callback oriented
• Forced to think about what to do about
callback
• Forces you to think if you need to wait for it
or continue
• Parallel friendly approach —> Default
22. Installing Node
• Directly, from nodejs.org
• Using nvm – Node Version Manager
– Easier to update, and switch versions
23. When to use NodeJS?
• IO intensive
– File, DB
• Web Servers
– More concurrent connections, cheaper!
• Scripts / Tools
– Command line toolbelt
24. When to avoid NodeJS
• CPU intensive
• Long running synchronous tasks
• Mathematical Operations – Floating
Numbers
28. Code Along – Initial Setup
Checkout:
git clone https://github.com/Fundoo-
Solutions/nodejsfoo.git
cd nodejsfoo
29. Code Along
Step 1 - Simple Hello World
Step 2 - Handling Arguments & Inputs
Step 3 - Using Inbuilt Libraries
Step 4 - Event Handling & Callbacks
Step 5 - Sharing Logic Between Files
30. Step 1 – Simple Hello World
• console.log
• With multiple arguments
• Running it
• node <filename>
31. Step 2 - Handling Arguments and
Inputs
• process.argv
• First two arguments
• node
• filename
• Then
• arguments in order from command line
• array of strings
32. Step 3 – Inbuilt Libraries - fs
• Pull in libraries using
• require()
• require(‘fs’);
• fs.readFileSync(filename, encoding);
• fs.readFile is better!
• Read as pure string!
45. Agenda
• Working with Node packages
• NPM & Requires
• Async
• Express
• Routes & Config
46. How does a package.json
look like?
{
"name": ”DEMO App",
"description": "DEMO App for Workshop",
"version": "0.0.1",
"private": true,
"engines": {
"node": "0.10.x",
"npm": "1.3.x"
},
"dependencies": {
"express" : "4.1.1",
"body-parser" : "1.0.2",
…
}
}
47. Package.json syntax &
versioning
• MAJOR.MINOR.PATCH
• “primus”: “1.2.3”
• Specific
• *, x - wildcard support
• “async”: “*”
• ~ - Atleast, in this minor release
• “async”: “~0.8.1”
• >= 0.8.1
• < 0.9.0
48. Package.json syntax &
versioning
• ^ - More relaxed versioning
• “async” : “^0.8.0”
• >= 0.8.0
• < 1.0.0
• Recommendation:
• Use ^, the default
49. Package.json – Dependencies vs
Dev Dependencies
• dependencies
• Always needed
• Production!
• devDependencies
• Same syntax
• Installed unless NODE_ENV=production
50. npm Scripts
• scripts: {}
• test, start, whateveryouwant
• Shell commands
• echo “Deploying…”
• node index.js
• Shortcuts
• Run using npm run
51. async
• For easier dealing with multiple asynchronous
calls
• Point in case, async.parallel
• An array or object with functions
• Each function takes a callback as parameter
• Once all functions are done, proceeds to a
final block
52. Code Along
Step 6 - Init your package.son
Step 7 - Install and use async
53. Step 6 - Initing Package.son
npm init
(And answer all the questions)
54. Step 7 – Use async
• npm install --save async
• Understanding async parallel
• read files in async.parallel
• Do composite work in final callback
55. Express
• A web server framework for NodeJS
• The de-facto web server for most NodeJS apps
• The E in MEAN stack
• Configurable, extensible
• Middleware based plugins
63. Step 9 – Create routes and
handlers
• Move logic into get(‘/)
• res.send(json);
• or res.json(json);
• Parametrized get (‘/:id’)
• Access using req.params.id
• res.status(400) if not found, along with json payload
64. Step 10 – Handling post
routes
• npm install --save body-parser
• app.use(bodyParser.urlencoded({extended: true}));
• app.use(bodyParser.json());
• app.use(express.static(__dirname + '/public'));
• app.post('/register', function(req, res) {});
• req.body
• Watch out for ordering of routes and collisions!
66. Express Routers - Advanced
• var router = express.Router(optionsObj)
• caseSensitive
• strict
• /foo and /foo/ same by default
• router.use
• router.get / post / …
• app.use(‘/api/test’, router);
67. Nesting Routers
• var router1 = express.Router(optionsObj)
• var router2 = express.Router(optionsObj)
• router1.get(‘/user’, …)
• router1.post(‘/user’/, …)
• router2.use(‘/api’, router1);
• /api/user routes will be created
68. Multiple Environments
• Common configuration
• Environment specific
• development
• production
• Environment variables + configured constants
• Secure Info -> Environment Variables
• Otherwise, in code, checked in
69. What can you configure?
• DB urls
• application urls
• social media integration
• Anything and everything
74. Configuration in NodeJS
• JSON Config files
• One per environment, one global
• Set environment variable before running app
• Read from process.env
• Load file
• export
75. A note about authentication
• Don’t reinvent the wheel
• Connect -> Middleware for common use cases
• Passport -> Out of the box authentication framework
for NodeJS & Express
• Support for DB backed
• FB, Google, Twitter OAuth
• And more!
76. Agenda
• Working with MongoDB & SQL
• Build & Deploy
• Best Practices
• Further Readings
85. Build & Deploy
• Grunt / Gulp
• Run the tests before deploying
• No node_modules
• Ship entire folder
• Run npm install at installation location
• Point at main entry point
• Environment driven!
86. Structuring your Express Codebase
• Folders
– api
• route level
• controller, spec, model, routes
– auth
• routes
• strategies
• common
– config
• passport
• express
• environment based
– views
– components
87. Keeping the server running
• NodeJS crashes and stops
• Use forever
• Or an external process to keep restarting it
88. Best Practices
• Structure, modules
• Parallelize when possible
• Think about it at every step
• Use libraries like async, underscore
• Search for existing npm packages
• Use Middlewares
• over calling functions in controllers and handlers
• ^ or ~ versions in package.json
• Otherwise keep it up to date on a monthly basis
89. Best Practices
• Follow the callback pattern
• Don’t break the err, data structure in your code
• Watch out for async behavior within loops
• Closure, but extract functions instead of inline
• Use javascriptlint
• or something similar
• Use forever, nodemon, or something similar
• To ensure your app doesn’t crash and burn
• Can use it locally as well Restart on file change
90. Other Useful Libraries
• async
• underscore
• RabbitMQ – for Queues and RPC Servers
• SocketIO / SockJS
92. AngularJS + NodeJS Chat App
• Setup Socket Listeners and Posters on NodeJS
• Using SocketIO
• Client side already setup
• io.on(‘connection’, function(socket) {});
• socket.on(‘event’, function(data) {});
• io.emit(‘event’, data)
• broadcast to all!
93. Further Reading
• Clusters
• RabbitMQ
• SocketIO
• vasync
• More observable, async
• restify
• Pure RESTful services, not web apps
94. Cluster Example
var cluster = require('cluster');
var http = require('http');
var numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
}
95. Cluster Example - continued
else {
// Workers can share any TCP connection
// In this case its a HTTP server
http.createServer(function(req, res) {
res.writeHead(200);
res.end("hello worldn");
}).listen(8000);
}
96. How it works
• Child processes spawned using fork
• Communicate with parent using IPC
• Pass server handles back and forth
• Server.listen in child
• Serializes arguments
• Passes to master process
• Handle to server created or passed to child
• OS responsible for load balancing across processes
sharing same resource
98. Thank you
• Contact
• shyam@hopscotch.in
• @omniscient1
• Come join me at Hopscotch if you want to work
on exciting stuff with a great company!
• WE ARE HIRING!
99. Hopscotch in Brief
• Customer focused, Content oriented, discovery oriented shopping experience for moms!
• Not an Amazon/Flipkart model
• Vertical focused
• Great leadership team
• Harvard, INSEAD, Amazon, Google…
• Amazing investors
• Eduardo (FB Co-founder), Velos Partners, and more!
• Customer focused, smart and rapid growth
• 20-30% month on month!
• Scrappy, hacker oriented culture
• Come be a part of something awesome!
Notas do Editor
When this over previous? Large files, process info as and when available. Logs, etc.