Markovtsev Vadim
Web Programming @ MIPT, 2015-2016
sudo apt-get install nodejs nodejs-legacy
brew install node
choco install nodejs
hello.js
:
console.log("hello, world");
node hello.js
while (true) {
process events;
}
Example of a blocking call:
contents = file.read();
next line will be executed after the file is read
Example of a non-blocking call:
file.read(function(contents) { ... });
next line will be executed at once, without waiting
globals
- global namespace (mainly used for writing)process
- current process instanceconsole
- similar to browsers, loggingrequire
, module
, exports
- import systemsetTimeout
, setInterval
and friendsEventEmitter
- building block of the events machinery
var EventEmitter = require('events');
let emitter = new EventEmitter();
emitter.on()
adds event handlersemitter.emit()
triggers eventsemitter.removeListener()
- removes event handlersBuffer
is binary data representationArray
interface, with slice
, length
, etc.var fs = require('fs');
*Sync
) function versionsfs.FSWatcher
uses
inotify /
FSEvents mechanisms to deliver file system change eventsstack
property allows finding the place where the exception was thrown"error"
event for pipesnode debug ...
allows interactive debugging of the script; alternatively, send SIGUSR1
signal to already running script
and use ‑p <PID>
switchdebugger
into your script to hit a breakpoint therebt
- print the backtracec
- continue execution, restart
- relaunch the scriptn
- step next, s
- step in, o
- step outwatch(expr)
- inspect the expression, be notified on changesfoo.js
:
exports.jack = "the sparrow";
bar.js
:
var foo = require("./foo.js");
console.log(foo.jack);
./
is important! Without it, the module is considered to be either
a built-in or existing in
node_modules.
package.json
with main
pointing to the
actual file to importindex.js
is probed automaticallyindex.node
is probed automaticallynode_modules
structure (npm v3 is about to change this)package.json
npm install <name>
npm uninstall <name>
npm search <name>
npm install
reads package.json
in the current
directory and installs dependencies and devDependencies to
node_modules
in the current directorynpm run ...
)package.json
can be created interactively using npm init
bower_components
bower.json
bower install <name>
bower uninstall <name>
bower search <name>
make
, the build definition is read from gulpfile.js
package.json:
..."scripts": {
"gulp": "gulp"
},
...
gulpfile.js
and specify devDependencies in package.json
npm run gulp
does the magicMake the same as npm dependency visualization but for Gulp.
Similar: gulp-graph-tasks. We can do better!
Tip: use gulp --tasks-json
Tip: do not bind to Gulp (use independent intermediate representation), so that the code can be reused for other languages (Python, Go, etc.)
Hint: do not forget about d3.js