Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Off the Rails
Search
Danielle Smith
March 17, 2017
Programming
0
26
Off the Rails
My journey to becoming a
~hipster~ Node.js developer
Danielle Smith
March 17, 2017
Tweet
Share
More Decks by Danielle Smith
See All by Danielle Smith
Ruby Ruby
daninithepanini
0
140
A Brief History of Ruby
daninithepanini
0
39
Static Type Inferencing ... in Ruby?
daninithepanini
0
21
Action Game
daninithepanini
0
35
RubyGL
daninithepanini
0
26
YeSQL
daninithepanini
0
24
Game Dev in Ruby
daninithepanini
0
36
Euler vs Hamilton
daninithepanini
0
40
Other Decks in Programming
See All in Programming
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
630
Google Agent Development Kit でLINE Botを作ってみた
ymd65536
2
240
Azure AI Foundryではじめてのマルチエージェントワークフロー
seosoft
0
160
Systèmes distribués, pour le meilleur et pour le pire - BreizhCamp 2025 - Conférence
slecache
0
120
“いい感じ“な定量評価を求めて - Four Keysとアウトカムの間の探求 -
nealle
1
9.3k
Claude Code + Container Use と Cursor で作る ローカル並列開発環境のススメ / ccc local dev
kaelaela
7
3.6k
VS Code Update for GitHub Copilot
74th
2
630
Hypervel - A Coroutine Framework for Laravel Artisans
albertcht
1
110
Hack Claude Code with Claude Code
choplin
4
1.5k
Deep Dive into ~/.claude/projects
hiragram
14
2.5k
5つのアンチパターンから学ぶLT設計
narihara
1
160
GraphRAGの仕組みまるわかり
tosuri13
8
530
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
7
730
Into the Great Unknown - MozCon
thekraken
40
1.9k
Building Applications with DynamoDB
mza
95
6.5k
The Language of Interfaces
destraynor
158
25k
How to Ace a Technical Interview
jacobian
277
23k
Code Review Best Practice
trishagee
69
18k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Art, The Web, and Tiny UX
lynnandtonic
299
21k
StorybookのUI Testing Handbookを読んだ
zakiyama
30
5.9k
How GitHub (no longer) Works
holman
314
140k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
49
5.5k
Music & Morning Musume
bryan
46
6.6k
Transcript
Off the Rails My journey to becoming a hipster Node.js
developer
Hi, I’m Dan
Ruby is my first love
But I got into an affair with Node.js
Temptation
Client said they had “experience”
(also didn’t seem to like ruby for some reason)
But what the heck, Node.js is the new hotness, right?
None
First rule of node.js -
THERE ARE NO RULES!
Seeing a node.js project for the first time
Controllers?
Express.js !
Models?
Find an ORM that works!
Views?
$ mkdir views $ vim views/index.jade
No Black Magic
Just raw, naked programming
Make your OWN conventions
None
“What happens server-side, stays server side” Problem?
Scenario: Developer A types in a ‘console.log’
Developer B: “where is that going to show up?”
Developer B: “where is that going to show up?” Developer
A: “in the server log”
Developer B: “where is that going to show up?” Developer
A: “in the server log” Developer B: “okay cool”
Developer B: “where is that going to show up?” Developer
A: “in the server log” Developer B: “okay cool” both devs watch the server log for some time
Developer B: “where is that going to show up?” Developer
A: “in the server log” Developer B: “okay cool” both devs watch the server log for some time Developer A: “No wait, there it is in Chrome’s devtools"
Familiar (but different)
npm / bundler
gulp / rake
ndenv / rbenv
mocha / rspec
jade / slim
Express.js / sinatra
Sequelize / ActiveRecord (almost)
factory-girl / factory-girl
Going so fast!
WHY SO SYNCHRONOUS?
Errorbacks
Errorbacks
The anatomy of an ‘errorback’ function(error, results…)
The anatomy of an ‘errorback’ function(error, results…) the result(s) of
the function taking the callback
The anatomy of an ‘errorback’ function(error, results…) this is non-null
if there was an error the result(s) of the function taking the callback
foo(42, function(err, result) { // happens “later” if (err) {
console.error(err.message); } else { console.log(result); } }); // happens immediately ...
Seems innocent enough until ...
Callback Hell
foo(42, function(err, fooResult) { if (!err) { bar(fooResult, function(err, barResult)
{ if (!err) { baz(barResult, function(err, bazResult) { if (!err) { qux(bazResult, function(err, quxResult) { if (!err) { norf(quxResult, function(err, norfResult) { if (!err) { console.log("Great Success!"); } else { console.error("Could not norf"); } }) } else console.error("Could not qux"); }); } else console.error("Could not baz"); }); } else console.error("Could not bar"); }); } else console.error("Could not foo"); });
Async Waterfall (not at all related to the development methodology)
async.waterfall([ function(cb) { foo(42, cb); }, bar, baz, qux, norf
], function(err, result) { if (!err) { console.log("Great Success!"); } else { console.error("Could not norf: " + err.message); } });
Better, but not perfect
A Promising Solution
foo(42) .then(bar) .then(baz) .then(qux) .then(norf) .then(function(norfResult) { console.log("Great Success!"); })
.catch(function(error) { console.error("Could not norf: " + err.message); });
Promises are like monads
User.find(42).then(function(user) { // returning a promise return user.getPosts(); }).then(function(posts) {
// posts is the resolved value });
User.find(42).then(function(user) { // returning a value return user.name; }).then(function(name) {
// name === user.name });
Functional.
User.find(42).then(function(user) { return user.getPosts(); }).filter(function(post) { return /Lorem Ipsum/.test(post.body); }).map(function(post)
{ return post.getComments(); }).reduce(function(total, comment) { return comment.getLikes() .then(function(likes) { return total + likes.length; }); }, 0);
Broken Promises...
User.find(42).then(function(user) { return user.getPosts(); }).then(function(posts) { // yay, I have
posts! // but where is my user? }).then(...); Bringing ‘user’ to the party
User.find(42).then(function(user) { return user.getPosts() .then(function(posts) { // yay, I have
posts! // and I have a user! )}; }).then(...); Promise Hell?
var user; User.find(42).then(function(_user_) { user = _user_; return user.getPosts() }).then(function(posts)
{ // yay, I have posts! // and I have a user! }).then(...); Klutzy variables?
var findUser = User.find(42); findUser.then(function(user) { return user.getPosts(); }).then(function(posts) {
var user = findUser.value(); // yay, I have posts! // and I have a user! }).then(...); ‘Resolved’ values?
var nizzleUserPosts = function(user) { return user.getPosts() .then(function(posts) { //
yay, I have posts! // and I have a user! }); } User.find(42).then(nizzleUserPosts) .then(...); Refactor that Shizz-Nizzle!
Regret?
None
Non-blocking means code sometimes runs off without you
The WTFs of Asynchronous-ness “I called this here and it
console.log’s there, wtf?” “Foo is happening before Bar, wtf?” “I called Bar, but it never happened, wtf?” “I never called Baz, but it’s happening, wtf?”
I’ve never missed ActiveRecord so much
Sequelize ORM
Migrations that needed to be migrated
Table “SequelizeMeta” has no column “to”
Associations that can’t associate
User.hasMany(Post); //... User.find(42, {include: Post}); //... Error: User and Post
not associated!
Source Maps while testing, anyone?
$ gulp test ... TypeError: evaluating ‘z[fn]’ /MyBlog/models/user.coffee:1:1 ... /MyBlog/models/user.coffee
1: ‘use strict’ 2: 3: module.exports = 4: foo: ->
This is not the require you’re looking for
Ruby: FooBar = require ‘foobar’ # Now you have FooBar
module # ... everywhere!
Node.js: FooBar = require(‘foobar’); // Now you have FooBar module
// ... in this file only
Circular dependency nightmares
require(‘foo’) in ‘bar’ require(‘bar’) in ‘foo’ you’re gonna have a
bad time
Sometimes I wish I could just `rails c`
Sometimes I wish I could just `binding.pry`
Acceptence
Express.js is fantastic
app.use(function(req, res, next) { // write some middlewarez next(); });
MIDDLEWARE ALL THE THINGS!
Authentication?
DB Connection?
Asset Pipeline?
`before` hook?
Middleware!
Node.js has come a long way
This ain’t yo mamma’s JavaScript
Walmart, PayPal, Ebay, Linkedin can’t be wrong, right?
Praised for its short development time
And IO performance (thanks to non-blocking)
ORMs and RDBMS support not quite there yet (IMO, don’t
quote me)
But huge NoSQL support (mongo, couch, etc)
Large, complex, enterprise-y software? maybe not
Microservices? Absolutely!
Thanks