Create the following file:
var http = require ('http' );
var server = http.createServer(function (request, response ) {
response.write('Welcome to Intro to Node.js!' );
response.end();
});
server.listen(8080 , function ( ) {
console .log('server listening at http://localhost:8080/' );
});
Run it from the terminal (command line):
$ node http-server.js
Point your browser to http://localhost:8080/
Help people get unstuck.
go 'til ~5min in Welcome to Intro to Node.js!
- chat about example code
- run it on the board Sequoia McDowell
@_sequoia | sequoia.mcdowell@gmail.com
- Programmer and Educator
- web tech for 10yrs, PHP + java
- almost all JS for n+ years
- Spent summer teaching Strongloop/ibm
- Asked to put on workshop for O'Reilly
- Who are you? About this workshop
- mix of lecture & hands-on
- ask questions, I may table
- help your neighbor! (or raise hand & I'll help)
- Goal: orient to Node.js
- know what's out there, comfortable working or starting project Agenda: Day 1
What is Node.js
Javascript Review
Core Modules
Understanding Async
The Node Ecosystem & NPM
Agenda: Day 2
Express.js
Mongo & Mongoose
SQL & Sequelize
Hands-on Exercises!
- talk about excercise slides & transition slides
- move fairly quickly, all code is available
Time: 9:15 What is Node.js?
"Javascript on the server"
Javascript Interpreter (V8)
Server APIs
libuv/Event Loop
- V8 is same as chrome
- no window
, DOM
, alert
, etc.
- whiteboard this ^ (venn diagram?) TODO: create illustration
- Server APIs = tools for the server (fs, http.server etc.)
- libuv is cross-platform bindings for async I/O <-- explain importance Why use Node.js?
It's fast
It's (becoming) ubiquitous
Same language as front-end
- eLoop Talk more about speed in a moment
- good for high volume, small payload API style
- "lingua franca" for webdev, replacing php, java, ruby
- reuse skills, sometimes even reuse code Why Not use Node.js?
CPU intensive tasks
Heavy investment in Java/PHP/etc.
Ecosystem currently in flux
- video processing, etc. (you know who you are)
- Use side-by-side
- Team or apps all Java/PHP/etc.
- Will be harder to find devs in future tho!
- This is changing
- With LTS this isn't so bad
- also, ES6 Key differences from Java/Ruby/PHP
Prototypal Inheritance
Single Threaded
Asynchronous I/O
No "Rails"
- You can write in classical style but it's not the "go-to" style
- Event loop
- Event loop again!
- There is no established front-runner framework Running Node
Run file
Node console
Executable script
NPM global install
- Who got the intro exercise working?
- Who didn't?
- File call bin, pass it file arg
- Node console - solicit 2 or 3 observations
- hashbang (time allowing)
- NPM global: we'll talk about this later
Run a script
Create hello-world.js
console .log('hello world' );
Run it
Using the console
Launch the node console
Run the following statements
var numbers = [72 , 101 , 108 , 108 , 111 , 32 , 87 , 111 , 114 , 108 , 100 ];
function decode (number ) { return String .fromCharCode(number); }
numbers.map(decode).join('' );
Up Next: What is Javascript
Create the following file:
//filename: welcome-to-node/done/hello-world-server.js
var http = require('http');
var server = http.createServer(function(request, response){
response.write('Welcome to Intro to Node.js!');
response.end();
});
server.listen(8080, function(){
console.log('server listening at http://localhost:8080/');
});
Run it from the terminal (command line):
$ node http-server.js
Point your browser to http://localhost:8080/
Help people get unstuck.
go 'til ~5min in