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
41
Other Decks in Programming
See All in Programming
Introducing ReActionView: A new ActionView-compatible ERB Engine @ Rails World 2025, Amsterdam
marcoroth
0
710
チームのテスト力を鍛える
goyoki
3
940
プロポーザル駆動学習 / Proposal-Driven Learning
mackey0225
2
1.3k
Kiroで始めるAI-DLC
kaonash
2
630
ファインディ株式会社におけるMCP活用とサービス開発
starfish719
0
2.1k
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
110
Platformに“ちょうどいい”責務ってどこ? 関心の熱さにあわせて考える、責務分担のプラクティス
estie
1
170
Testing Trophyは叫ばない
toms74209200
0
890
Performance for Conversion! 分散トレーシングでボトルネックを 特定せよ
inetand
0
3.4k
Flutter with Dart MCP: All You Need - 박제창 2025 I/O Extended Busan
itsmedreamwalker
0
150
そのAPI、誰のため? Androidライブラリ設計における利用者目線の実践テクニック
mkeeda
2
2.8k
デザイナーが Androidエンジニアに 挑戦してみた
874wokiite
0
570
Featured
See All Featured
GraphQLの誤解/rethinking-graphql
sonatard
72
11k
Code Reviewing Like a Champion
maltzj
525
40k
How to Think Like a Performance Engineer
csswizardry
26
1.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.9k
Embracing the Ebb and Flow
colly
87
4.8k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
252
21k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Speed Design
sergeychernyshev
32
1.1k
Documentation Writing (for coders)
carmenintech
74
5k
Java REST API Framework Comparison - PWX 2021
mraible
33
8.8k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
Faster Mobile Websites
deanohume
309
31k
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