Node.js has given JavaScript a new resurgence as a server-side language. No longer just for image rollovers and AJAX, JS is now available as a platform for creating lightning-fast, lightweight, networked applications. In this session, we will move beyond Node’s base web servers and Twitter applications, and into module development: those small, reusable components that are the foundation for every business application on every platform. Learn how to create a module within Node.js, how to test your module and validate functionality, and how to get your creation distributed into the wild. With this knowledge, you can make the next great Node package and become famous.
21. $
npm
init
name: (sample-node) Sample
version: (0.0.0) 0.1.0
description: This is a sample module
entry point: (index.js) _
22. author:
Jay
Harris
license:
(BSD)
BSD
About
to
write
to
/Projects/sample-‐node/package.json:
{
"name":
"Sample",
"version":
"0.1.0",
"description":
"This
is
a
sample
module",
"main":
"index.js",
"scripts":
{
"test":
"echo
"Error:
no
test
specified"
&&
exit
1"
},
"author":
"Jay
Harris",
"license":
"BSD"
}
Is
this
ok?
(yes)
_
31. var person = require("./lib/person.js");
// This will return undefined
person.firstName;
// This will return 'Jay'
var jayHarris = new Person('Jay','Harris');
jayHarris.firstName;
32. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Person(first, last) {
// ...
}
module.exports = Person;
// This is a public method;
Person.prototype.sayHello = function() {
return _join.call(this, "Hello", this.firstName);
}
// This is a private method
var _join(first, second) {
return first + ' ' + second;
}
33. var
person
=
require("./lib/person.js");
//
This
will
throw
'Has
No
Method'
error
person.sayHello();
//
This
will
return
'Hello
Jay'
var
jayHarris
=
new
Person('Jay','Harris');
jayHarris.sayHello();
34. 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function Person(first, last) {
// ...
}
module.exports = Person;
// This is a static method
module.exports.sayHello = function() {
return "Hello World!";
}
// This is a public instance method;
Person.prototype.sayHello = function() {
return _join.call(this, "Hello", this.firstName);
}
// This is a private method
var _join(first, second) {
return first + ' ' + second;
}
35. var
person
=
require("./lib/person.js");
//
This
will
return
'Hello
World!'
person.sayHello();
//
This
will
return
'Hello
Jay'
var
jayHarris
=
new
Person('Jay','Harris');
jayHarris.sayHello();
41. var
person
=
require("./person.js");
//
This
will
return
'Hello
Jay'
var
jayHarris
=
new
Person('Jay','Harris');
jayHarris.on("sleep",
function()
{
console.log("Goodnight,
Jay");
}
jayHarris.goToBed();
//
Output
'Goodnight,
Jay'
43. “
TDD is to coding style as yoga is
to posture. Even when you're not
actively practicing, having done so
colors your whole life healthier.
”
j. kerr
49. $
node
test.js
assert.js:104
throw
new
assert.AssertionError({
^
AssertionError:
false
==
true
at
Object.<anonymous>
(my-‐test.js:7:8)
at
Module._compile
(module.js:449:26)
at
Object.Module._extensions..js
(module.js:467:10)
at
Module.load
(module.js:356:32)
at
Function.Module._load
(module.js:312:12)
at
Module.runMain
(module.js:487:10)
at
process.startup.processNextTick.process._tick...
$
_
74. $
mocha
-‐-‐reporter
spec
Assertions
✓
should
pass
on
truthiness
1)
should
fail
on
falsiness
✖
1
of
2
tests
failed:
1)
Assertions
should
fail
on
falsiness:
AssertionError:
false
==
true
at
(stack
trace
omitted
for
brevity)
$
_
76. groupSyntax
describe('Testing
out
this
thing',
function()
{
describe('with
a
subset
of
this
other
thing',
function()
{
it('should
do
stuff',
function()
{
//
Assertion
tests
});
});
});
78. $
mocha
-‐-‐reporter
spec
Assertions
of
equality
✓
should
pass
on
truthiness
✓
should
pass
on
falsiness
on
type
✓
should
pass
on
number
✔
3
tests
complete
(6ms)
$
_
82. $
mocha
-‐-‐reporter
spec
Assertions
of
truthiness
✓
should
pass
on
truthiness
✓
should
pass
on
falsiness
of
type
✓
should
pass
on
number
-‐
should
pass
on
object
✔
4
tests
complete
(6ms)
•
1
test
pending
$
_
86. $
make
test
Assertions
of
truthiness
✓
should
pass
on
truthiness
✓
should
pass
on
falsiness
of
type
-‐
should
pass
on
number
-‐
should
pass
on
object
✔
4
tests
complete
(6ms)
•
2
test
pending
$
_
99. $
make
test
Assertions
✓
should
pass
on
truthiness
1)
should
fail
on
falsiness
✖
1
of
2
tests
failed:
1)
Assertions
should
fail
on
falsiness:
AssertionError:
false
==
true
at
(stack
trace
omitted
for
brevity)
$
_
101. $
npm
test
Assertions
✓
should
pass
on
truthiness
1)
should
fail
on
falsiness
✖
1
of
2
tests
failed:
1)
Assertions
should
fail
on
falsiness:
AssertionError:
false
==
true
at
(stack
trace
omitted
for
brevity)
$
_
142. $
npm
publish
npm
ERR!
Error:
This
package
has
been
marked
as
private
npm
ERR!
Remove
the
'private'
field
from
the
package.json
to
publish
it.
$
_