Jordan Kasper | @jakerella
"JavaScript" is a trademarked term!
...with various implementations:
Node builds on V8 with C++ and JavaScript code.
And specifically ones with high concurrency that interact with multiple back ends.
(But that's ok...)
npm
"Node Package Manager"
More than just dependencies!
package.json
/my-project$ npm init
This utility will walk you through creating a package.json
file. It only covers the most common items, and tries to
guess sane defaults.
...
Press ^C at any time to quit
name: (my-package-name)
package.json
{
"name": "my-project",
"version": "1.0.0",
"description": "This is my project!",
"main": "main.js",
"scripts": {
"test": "echo \"You have no tests!\""
},
"author": "John Doe <john@doe.com>",
"license": "MIT",
"private": true
}
/my-project$ npm install express --save
/my-project$ npm install mocha --save-dev
{
...,
dependencies: {
"express": "^4.12.4"
},
devDependencies: {
"mocha": "^2.3.3"
}
}
/my-project$ npm install express --save
my-project
|_ node_modules
|_ express
|_ mocha
|_ bluebird
...
|_ server
...
Node encourages Semantic Versioning (semver)...
4 . 2 . 1
major minor patch
When adding a dependency in Node:
dependencies: {
"express": "^4.12.4"
},
"^4.12.4" >> 4.12.5, 4.12.6, 4.13.0, 4.14.1, 5.0.0
"~4.12.4" >> 4.12.5, 4.12.6, 4.13.0, 4.14.1, 5.0.0
"4.12.*" >> 4.12.5, 4.12.6, 4.13.0, 4.14.1, 5.0.0
"*" >> ಠ_ಠ
Used for various tooling
(scaffolding, testing, build, deploy, etc)
~$ npm install -g grunt-cli
~/my-project$ grunt build
READ THE DOCS.
Doesn't have docs? Use a different module.
var express = require('express');
var app = express();
app.set('foo', 'bar');
Modules can live different places:
var filesystem = require('fs'), // core Node module
express = require('express'), // from /node_modules/
router = require('./app/router.js'), // app file
config = require('../config/config.json');
// hello-world.js
module.exports = function sayHello( recipient ) {
console.log( "Hello " + recipient );
};
// main.js
var foobar = require('./hello-world');
foobar( 'Everyone!' );
$ node main.js
Hello Everyone!
Whatever is on the module.exports
property becomes
the return value of require('...')
!
//answer.js
module.exports = 42;
// main.js
var theAnswer = require('./answer.js');
theAnswer === 42; // true!
// company.js
module.exports = {
name: "Foo's Widgets",
sayHello: function() {
return "Hello " + this.name;
}
};
var api = require('./company');
console.log( api.sayHello() ); // "Hello Foo's Widgets"
Modules are cached by Node!
Requiring a module twice could yield unexpected results!
module.exports = {
salary: 50000,
giveRaise: function(amount) {
this.salary += amount;
}
};
var workerOne = require('./worker');
workerOne.giveRaise(10000);
console.log( workerOne.salary ); // 60000
var workerTwo = require('./worker');
workerTwo.giveRaise(10000);
console.log( workerTwo.salary ); // 70000!
module.exports = function createWorker(options) {
return {
salary: options.salary,
giveRaise: function(amount) {
this.salary += amount;
}
};
};
var getWorker = require('./worker');
var workerOne = getWorker({ salary: 30000 });
workerOne.giveRaise(7000); // workerOne.salary === 37000
var workerTwo = getWorker({ salary: 50000 });
workerTwo.giveRaise(5000); // workerTwo.salary === 55000
Use JavaScript prototypes and constructor functions to construct objects.
var Worker = module.exports = function Worker(options) {
// ... worker initialization stuff
};
Worker.prototype.giveRaise = function(amount) {
this.salary += amount;
};
process
An interface to the current Node system process:
process.env
)process.argv
)process.pid
)process.memoryUsage()
)var http = require( 'http' );
var server = http.createServer( function request( req, res ) {
res.writeHead( 200, {
'Content-Type': 'text/html'
} );
res.end( '<h1>Hello World!</h1>' );
} );
server.listen( 3000, '127.0.0.1', function() {
console.log( 'The server is up!' );
} );
var fs = require( 'fs' );
fs.readFile( 'config.json', function( err, data ) {
if ( err ) {
// handle it!
console.error( err.stack );
return;
}
// `data` is a Buffer, so decode it first...
console.log( JSON.parse( data.toString('utf-8') ) );
});
Easy to connect to most database management systems
Native drivers for:
~/my-app$ npm install mysql --save
var mysql = require('mysql');
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
// ...
});
connection.connect();
connection.query(
"SELECT * FROM `books` WHERE author='Tolstoy'",
function(err, rows, fields) {
if (err) { /* ... */ return; }
rows.forEach(function(row) { /* ... */ });
connection.end();
}
);
Iknowright?
jordankasper.com/intro-to-node
Jordan Kasper | @jakerella