require
module.exports
require
require
lookup behaviorrequire(X);
node_modules
?var http = require('http');
var url = require('url');
var parser = require('./lib/parser.js');
var Assembler = require('../Assembler');
var users = require('../data/users.json');
Relative to thisFile.js
\-- project
+-- Assembler.js
+-- data
| \-- users.json
|
\-- src
+-- thisFile.js
\-- lib
\-- parser.js
var cars = require('../../cars');
var util = require('./util');
+-- cars
| +-- package.json
| \-- ...
|
\-- project
\-- src
+-- thisFile.js
\-- util
\-- index.js
node_modules
var _ = require('lodash');
var express = require('express');
\-- project
+-- package.json
+-- node_modules
| + -- lodash
| | \-- ...
| |
| \ -- express
| \-- ...
|
|-- thisFile.js
\-- src
\-- thisFile.js
require
lookup behaviornode_modules
?...Works the same way
.
+-- myDir
| +-- package.json
| +-- index.js
| \...
|
+-- foo
| +-- index.js
| \...
|
+-- foo.js
\-- index.js
$ node ./foo.js
$ node .
$ node myDir
$ node foo
node-ecosystem/start/require-xyz/index.js
path/to/node-ecosystem/done/require-xyz/x.js
path/to/node-ecosystem/done/require-xyz/y.js
path/to/node-ecosystem/done/require-xyz/a/b/index.js
Amici's
Taco Gong
Unfriendly's
Shake Hovel
Grey Castle
Hints:
.js
file logs its pathjson
files can be loaded with require
Up Next: module.exports
module.exports
// favenum.js
module.exports = 1738;
// consumer.js
var favoriteNumber = require('./favenum.js');
console.log(favoriteNumber); // 1738
var foo = require('./foo.js');
//basically means...
var foo = {{module.exports from the file 'foo.js'}}
module.exports = function add(x, y){ return y + x }
module.exports = "Hi there";
function Singleton(){
//...
}
module.exports = new Singleton();
a.js
that exports a functionb.js
to require
it.b.js
Hints:
{}
)// myMaths.js
module.exports = {
add : function(x, y) { return x + y; },
sub : function(x, y) { return x - y; },
div : function(x, y) { return x / y; },
mul : function(x, y) { return x * y; }
/*...*/
}
module.exports.add = function(x, y) { return x + y; };
module.exports.sub = function(x, y) { return x - y; };
module.exports.div = function(x, y) { return x / y; };
module.exports.mul = function(x, y) { return x * y; };
// mathsConsumer.js
var maths = require('./myMaths.js');
console.log(maths.add(4, 6)); // 10
console.log(maths.div(9, 0)); // Infinity
// fsConsumer.js
var readFile = require('fs').readFile;
readFile('foo.txt', 'utf-8', function(err, contents){
//...
// Car.js
function Car(make, model){
this.make = make;
this.model = model;
}
module.exports = Car;
// carConsumer.js
var Car = require('./Car')
var ride = new Car('Toyota', 'Celica');
console.log(JSON.stringify(ride));
//logger.js
module.exports = function(prefix){
function format(msg){
return prefix + ' <' + Date() + '> ' + msg;
}
return {
l : function(msg){ console.log(format(msg)) },
e : function(msg){ console.error(format(msg)) },
};
}
// loggerConsumer.js
logger = require('./logger.js')('MyApp');
logger.l('testing 123');
// MyApp <Wed Jan 13 2016 16:54:55 GMT-0500 (EST)> testing 123
node-ecosystem/start/multi-log.js
logger.js
moduleHints:
node-ecosystem/misc/logger-consumer.js
for an exampleExtra Credit:
logger.js
so loggers are enabled via environment variablelogfactory = require('./logger.js');
dblog = logfactory('DATABASE');
applog = logfactory('MYAPP');
applog.l('boostraping...');
// MYAPP <Wed Jan 13 2016 16:54:55 GMT-0500 (EST)> bootstrapping...
dblog.l('SELECT * FROM users');
// DATABASE <Wed Jan 13 2016 16:54:55 GMT-0500 (EST)> SELECT * FROM users
// dog-stuff.js
module.exports = {
name : 'Dog',
dogNoise : 'Bark!',
bark : function(){
console.log(this.dogNoise);
}
}
// dogUser.js
dog = require('./dogStuff');
dog.bark(); // Bark!
// dog-stuff.js
module.exports = {
name : 'Dog',
noise : 'Bark!',
bark : function(){
console.log(this.noise);
}
}
// perro.js
perro = require('./dogStuff');
perro.noise = '¡guau guau!'
// dogUser.js
dog = require('./dogStuff');
require('./perro');
dog.bark(); // ??
Up Next: Excercise!
Write greeter-factory.js
for...
// node-ecosystem/start/server-with-module.js
// GET http://localhost:8080/
// ==> 'Hello World!'
var http = require('http');
var factory = require('./greeter-factory');
var handler = factory('Hello World!');
var server = http.createServer(handler);
server.listen(8080);
Hints:
handler
? What is factory
?http.createServer
take?/
to verifyUp Next: NPM