Web Programming @ MIPT, 2015-2016

Markovtsev Vadim

node.js

Web Programming @ MIPT, 2015-2016

What is node.js?

Official site

Installation

Example

hello.js:
			console.log("hello, world");
		
node hello.js

Asynchronous execution in node.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

Documentation on global objects

Events

Documentation on events Example of deriving from EventEmitter

Buffers

Documentation on buffers

File system

Documentation on fs

Exceptions

Documentation on global objects

Debugging

Documentation on global objects

Import system

Documentation on modules

foo.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.

Importing packages

Documentation on package import There are 3 ways to make node.js treat a folder as a module:
  1. Define package.json with main pointing to the actual file to import
  2. index.js is probed automatically
  3. index.node is probed automatically

Node package manager

Official site
			npm install <name>
			npm uninstall <name>
			npm search <name>
		

package.json

Documentation

Bower package manager

Official site
			bower install <name>
			bower uninstall <name>
			bower search <name>
		

Gulp

Official site

Build pipeline story

package.json:

...
"scripts": {
  "gulp": "gulp"
},
...
			

Project idea

Make 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

Next steps

View on Github