Sunday, December 1, 2013

Node.js school


http://nodeschool.io/
>npm set http-proxy https://http.proxy.test.com:8000 --global
>npm install -g learnyounode
>learnyounode
>learnyounode select hello world

Write a program that prints the text "HELLO WORLD" to the console (stdout).

HINTS: To make Node.js program, create a new file with a `.js` extension and start writing JavaScript! Execute your program by running it with the `node` command. e.g.:
Created file1.js:
var http = require('http');
http.createServer(function (req, res) {
       res.writeHead(200, {'Content-Type': 'text/plain'});
       res.end('I Have arrived!\n');
}) .listen(1337, '127.0.0.1');
console.log('Node Server running at http://127.0.0.1:1337/');
>node file1.js
Node Server running at http://127.0.0.1:1337/
I Have arrived!

>learnyounode select my first i/o!

Write a program that uses a single synchronous filesystem operation to read a file and print the number of newlines it contains to the console (stdout), similar to running `cat file | wc -l`.
The full path to the file to read will be provided as the first command-line argument.

HINTS:
To perform a filesystem operation you are going to need the `fs` module from the Node core library. To load this kind of module, or any other "global" module, use the following incantation:
   var fs = require('fs')

Now you have the full `fs` module available in a variable named `fs`.
All synchronous (or blocking) filesystem methods in the `fs` module end with 'Sync'. To read a file, you'll need to use `fs.readFileSync('/path/to/file')`. This method will return a `Buffer` object containing the complete contents of the file.

Documentation on the `fs` module can be found by pointing your browser here:
C:\nodehome\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\fs. html

`Buffer` objects are Node's way of efficiently representing arbitrary arrays of data, whether it be ascii, binary or some other format. `Buffer` objects can be converted to strings by simply calling the `toString()` method on them. e.g. `var str = buf.toString()`.

Documentation on `Buffer`s can be found by pointing your browser here: C:\nodehome\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\buf fer.html

If you're looking for an easy way to count the number of newlines in a string, recall that a JavaScript `String` can be `.split()` into an array of substrings and that '\n'. Using this method you'll end up with an array that has one more element than the number of newlines.
var fs = require('fs');
var buf = fs.readFileSync('C:/logs/test.log');
var str = buf.toString();
var newstring = str.split('\n');
console.log('split ' + newstring);
var n = newstring.length -1;
console.log('count in file ' + n);


>learnyounode run file2.js Running "my first i/o!"...

 >learnyounode list

  • HELLO WORLD
  • BABY STEPS
  • MY FIRST I/O!
  • MY FIRST ASYNC I/O!
  • FILTERED LS
  • MAKE IT MODULAR
  • HTTP CLIENT
  • HTTP COLLECT
  • JUGGLING ASYNC
  • TIME SERVER
  • HTTP FILE SERVER
  • HTTP UPPERCASERER
  • HTTP JSON API SERVER





No comments:

Post a Comment