Agenda
- Why node.js?
- Node JavaScript Runtime Basics
- Routing and templating with Express
- Persistence Options
- Realtime Apps with Socket.io
- Sharing Code and Culture with the Client with Browserify
- Further Reading
doing is for doers
- This is a workshop for DOers
- Lots of live code
- Lots of hands-on experimentation
why might I want to use node?
JavaScript Everywhere
- Code reuse (minimal)
- Same programming culture on client and server
- Lots of JavaScript programmers
Vibrant Community
- 60,000+ packages on npm
- Top corporate sponsors
- Huge wealth of JS code
Sweet Spots
- Real-time/high concurrency apps (I/O Bound)
- API tier for single-page apps and rich clients (iOS, Android)
- Service orchestration
- Working with document (JSON-style) databases
Not So Sweet Spots (For Now)
- Simple CRUD Apps (Frameworks not as good)
- Not as fast as some for CPU-intensive stuff
What is node.js?
- v8 JavaScript runtime
- Event driven
- Non-blocking standard libraries
- Most APIs speak Streams
- Extensible via C/C++ add-ons
- Provides a package manager and module system for JS/native extensions
What node is not
- A framework like Django or Rails
- A new programming language
Interactive Mode
- Great for quick tests
- [Demo - node console]
Standard libraries
- Async by default
- Low level
- HTTP is first class citizen
- [Demo - hello world]
Developing and Debugging
- Lots of different choices
- nodemon (faster cycles)
- node-inspector (step-through debugging)
- [Demo - supervisor and debugging]
CommonJS Modules
- Prevents pollution of the global scope
- Two Styles of module development
- [Demo - your own local modules]
npm
- node's package manager (ruby gems)
- handles and resolves dependencies (Bundler)
- ~60,000 modules available today (easy to publish)
- [Demo - npm init and shameless plug]
Routing and templating with express
Express at a high level
- Based on Connect (a bit like Rack)
- De facto standard framework of node web apps
- Minimalistic, unopinionated
- Supports a variety of middleware
- Alternatives: hapi, restify, geddy, koa
Routes
- Respond to HTTP requests with a callback
- Supports variable placement in routes
- Easy to serve JSON
- [Demo - some basic routing and HTTP testing in Chrome]
Middleware
- Some useful built in middleware (like bodyParser)
- Useful for preprocessing requests
- Authentication/Authorization
- [Demo - some simple middleware, and how to use it]
Popular node.js options
- SQL libraries supported, but not especially popular
- Best community support is around Postgres (JSON column)
- Document stores more popular, good fit for JS
- MongoDB or CouchDB are popular primary durable data stores
- redis is popular for fast, in-memory data storage
Mongoose Concepts: Schema
- MongoDB by default enforces no data format
- Mongoose does with the "Schema" object
- Schema: define data types, validation, instance, and "static" functions
Mongoose Concepts: Model
- Representation of a MongoDB document
- Synthesized from a Schema
- You use the Model to actually create, query, and modify documents
Real time apps with socket.io
Socket.io at a high level
- Thin wrapper around WebSockets
- Works in tandem with an HTTP Server
- Documentation: less than awesome, wiki is better than site
Key Features
- Client/Server Push
- Broadcast
- Client "session" information
- Message namespaces
- Plain ol' WebSockets API as well
Sharing Code and Culture with Browserify
What is Browserify?
- node-style require in the browser!
- self-contained node module environment
- use local, core, or npm-installed modules
Why would I want to use Browserify?
- Helps you write modular code on the client
- no futzing with client-side script loading
- share client and server code
- Have the same code style, package management, and module system as server! Why have two different paradigms?
Fun Browserify Features
- Provides browser shims for lots of node core
- can provide modules with alternate entry points when used by browserify
- Source Maps
VPS Options
- Have had good experiences with Linode
- Use the Node KO stack script
- More info
- Also hear good things about Digital Ocean
PaaS Options
- Heroku
- Nodejitsu
- AppFog
- Red Hat OpenShift
More Exercises and Reinforcement
@shubhrakar
skar@strongloop.com