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
29
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
40
Static Type Inferencing ... in Ruby?
daninithepanini
0
23
Action Game
daninithepanini
0
37
RubyGL
daninithepanini
0
28
YeSQL
daninithepanini
0
25
Game Dev in Ruby
daninithepanini
0
37
Euler vs Hamilton
daninithepanini
0
44
Other Decks in Programming
See All in Programming
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
PostgreSQLで手軽にDuckDBを使う!DuckDB&pg_duckdb入門/osc25hi-duckdb
takahashiikki
0
230
Go コードベースの構成と AI コンテキスト定義
andpad
0
150
SQL Server 2025 LT
odashinsuke
0
120
The Past, Present, and Future of Enterprise Java
ivargrimstad
0
580
Rubyで鍛える仕組み化プロヂュース力
muryoimpl
0
300
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
AI時代を生き抜く 新卒エンジニアの生きる道
coconala_engineer
1
500
Basic Architectures
denyspoltorak
0
150
AI Agent Tool のためのバックエンドアーキテクチャを考える #encraft
izumin5210
5
1.5k
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
HTTPプロトコル正しく理解していますか? 〜かわいい猫と共に学ぼう。ฅ^•ω•^ฅ ニャ〜
hekuchan
2
580
Featured
See All Featured
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
37
Facilitating Awesome Meetings
lara
57
6.7k
Imperfection Machines: The Place of Print at Facebook
scottboms
269
13k
Code Review Best Practice
trishagee
74
19k
Being A Developer After 40
akosma
91
590k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
130
The SEO identity crisis: Don't let AI make you average
varn
0
43
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
39
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
34
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
2.8k
YesSQL, Process and Tooling at Scale
rocio
174
15k
WCS-LA-2024
lcolladotor
0
400
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