GithubHelp home page GithubHelp logo

max-mapper / art-of-node Goto Github PK

View Code? Open in Web Editor NEW
9.7K 9.7K 859.0 1.71 MB

:snowflake: a short introduction to node.js

Home Page: https://github.com/maxogden/art-of-node#the-art-of-node

License: Other

JavaScript 100.00%

art-of-node's People

Contributors

andyxning avatar awayken avatar chesstrian avatar climbsrocks avatar ericaxu avatar ferusinfo avatar fvntr avatar hristocodes avatar imkven avatar jeffpaine avatar lucasmoreiradev avatar max-mapper avatar maxdavila avatar mgriffin avatar monkindey avatar monthypython avatar mortonfox avatar oldo avatar readmecritic avatar sdlyu avatar sejoker avatar shawnhansen avatar shime avatar stagas avatar sun1one avatar taka-wang avatar timhudson avatar tomayac avatar tonygoussot avatar zeke avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

art-of-node's Issues

Why not use GitHub pages to host this doc?

Hey,
Why not use GitHub pages to host this doc? That way you can both maintain a repository and also get a working site for displaying this doc. BTW this doc is awesome.

RequireBin example error

Hey! Thanks so much for this guide.

The 'more complicated example' on RequireBin seems to have an error.
This: http://requirebin.com/?gist=6031068 results in

Uncaught TypeError: Cannot read property 'content' of undefined bundle.js:2 

Not sure what the fix is I'm afraid, but I figured you'd like to know.
Thanks again ✨

I get this error after running npm install learnyounode -g. Ubuntu 14.04

System.log debug file
0 info it worked if it ends with ok
1 verbose cli [ 'node', '/usr/bin/npm', 'install', 'learnyounode', '-g' ]
2 info using [email protected]
3 info using [email protected]
4 verbose node symlink /usr/bin/node
5 silly cache add args [ 'learnyounode', null ]
6 verbose cache add spec learnyounode
7 silly cache add parsed spec { raw: 'learnyounode',
7 silly cache add scope: null,
7 silly cache add name: 'learnyounode',
7 silly cache add rawSpec: '',
7 silly cache add spec: '',
7 silly cache add type: 'range' }
8 verbose addNamed learnyounode@

9 silly addNamed semver.valid null
10 silly addNamed semver.validRange *
11 silly addNameRange { name: 'learnyounode', range: '', hasData: false }
12 silly mapToRegistry name learnyounode
13 silly mapToRegistry using default registry
14 silly mapToRegistry registry https://registry.npmjs.org/
15 silly mapToRegistry uri https://registry.npmjs.org/learnyounode
16 verbose addNameRange registry:https://registry.npmjs.org/learnyounode not in flight; fetching
17 verbose request uri https://registry.npmjs.org/learnyounode
18 verbose request no auth needed
19 info attempt registry request try #1 at 04:16:17
20 verbose request id 2277823b8d017a85
21 http request GET https://registry.npmjs.org/learnyounode
22 http 200 https://registry.npmjs.org/learnyounode
23 silly get cb [ 200,
23 silly get { date: 'Mon, 05 Jan 2015 12:16:18 GMT',
23 silly get server: 'CouchDB/1.5.0 (Erlang OTP/R16B03)',
23 silly get etag: '"DYQOKPXSB3NA610ZBIFHCPWGO"',
23 silly get 'content-type': 'application/json',
23 silly get 'cache-control': 'max-age=60',
23 silly get 'content-length': '67738',
23 silly get 'accept-ranges': 'bytes',
23 silly get via: '1.1 varnish',
23 silly get age: '0',
23 silly get 'x-served-by': 'cache-sjc3127-SJC',
23 silly get 'x-cache': 'MISS',
23 silly get 'x-cache-hits': '0',
23 silly get 'x-timer': 'S1420460178.028819,VS0,VE246',
23 silly get vary: 'Accept',
23 silly get 'keep-alive': 'timeout=10, max=50',
23 silly get connection: 'Keep-Alive' } ]
24 silly addNameRange number 2 { name: 'learnyounode', range: '
', hasData: true }
25 silly addNameRange versions [ 'learnyounode',
25 silly addNameRange [ '0.0.0',
25 silly addNameRange '0.0.1',
25 silly addNameRange '0.0.2',
25 silly addNameRange '0.0.3',
25 silly addNameRange '0.0.4',
25 silly addNameRange '0.0.5',
25 silly addNameRange '0.0.6',
25 silly addNameRange '0.0.7',
25 silly addNameRange '0.0.8',
25 silly addNameRange '0.0.9',
25 silly addNameRange '0.1.0',
25 silly addNameRange '0.1.1',
25 silly addNameRange '0.2.0',
25 silly addNameRange '0.2.1',
25 silly addNameRange '0.2.2',
25 silly addNameRange '0.2.3',
25 silly addNameRange '0.2.4',
25 silly addNameRange '0.2.5',
25 silly addNameRange '0.2.6',
25 silly addNameRange '0.2.7',
25 silly addNameRange '0.2.8',
25 silly addNameRange '0.2.9',
25 silly addNameRange '0.2.10',
25 silly addNameRange '0.2.11',
25 silly addNameRange '0.2.12',
25 silly addNameRange '0.2.13',
25 silly addNameRange '0.2.14',
25 silly addNameRange '0.2.15',
25 silly addNameRange '0.2.16',
25 silly addNameRange '0.2.16-mimbug',
25 silly addNameRange '0.3.0',
25 silly addNameRange '0.3.1',
25 silly addNameRange '0.3.2',
25 silly addNameRange '0.3.3',
25 silly addNameRange '0.4.0',
25 silly addNameRange '0.4.1',
25 silly addNameRange '1.0.0-alpha01',
25 silly addNameRange '1.0.0',
25 silly addNameRange '1.0.1',
25 silly addNameRange '1.0.2',
25 silly addNameRange '1.0.3',
25 silly addNameRange '1.0.4',
25 silly addNameRange '1.0.5',
25 silly addNameRange '1.0.6',
25 silly addNameRange '1.0.7' ] ]
26 verbose addNamed [email protected]
27 silly addNamed semver.valid 1.0.7
28 silly addNamed semver.validRange 1.0.7
29 silly mapToRegistry name learnyounode
30 silly mapToRegistry using default registry
31 silly mapToRegistry registry https://registry.npmjs.org/
32 silly mapToRegistry uri https://registry.npmjs.org/learnyounode
33 verbose addRemoteTarball https://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz not in flight; adding
34 verbose addRemoteTarball [ 'https://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz',
34 verbose addRemoteTarball 'bea0a40c7e59823f543f5ed2a9bb1c4d6df6eb09' ]
35 info retry fetch attempt 1 at 04:16:18
36 info attempt registry request try #1 at 04:16:18
37 http fetch GET https://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz
38 http fetch 200 https://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz
39 silly fetchAndShaCheck shasum bea0a40c7e59823f543f5ed2a9bb1c4d6df6eb09
40 verbose addTmpTarball /tmp/npm-4975-da0d0bea/registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz not in flight; adding
41 verbose addTmpTarball already have metadata; skipping unpack for [email protected]
42 silly cache afterAdd [email protected]
43 verbose afterAdd /home/yonas/.npm/learnyounode/1.0.7/package/package.json not in flight; writing
44 verbose afterAdd /home/yonas/.npm/learnyounode/1.0.7/package/package.json written
45 silly install resolved [ { name: 'learnyounode',
45 silly install resolved version: '1.0.7',
45 silly install resolved description: 'Learn You The Node.js For Much Win! An intro to Node.js via a set of self-guided workshops.',
45 silly install resolved author:
45 silly install resolved { name: 'Rod Vagg',
45 silly install resolved email: '[email protected]',
45 silly install resolved url: 'https://github.com/rvagg' },
45 silly install resolved contributors: [ [Object], [Object], [Object], [Object], [Object], [Object] ],
45 silly install resolved repository:
45 silly install resolved { type: 'git',
45 silly install resolved url: 'https://github.com/rvagg/learnyounode.git' },
45 silly install resolved license: 'MIT',
45 silly install resolved dependencies:
45 silly install resolved { workshopper: '^1.0.0-alpha05',
45 silly install resolved 'workshopper-exercise': '^0.2.2',
45 silly install resolved 'workshopper-wrappedexec': '^0.1.1',
45 silly install resolved 'workshopper-boilerplate': '0.0.1',
45 silly install resolved 'concat-stream': '^1.4.1',
45 silly install resolved duplexer: '^0.1.1',
45 silly install resolved through: '^2.3.4',
45 silly install resolved boganipsum: '^0.1.0',
45 silly install resolved hyperquest: '^0.2.0',
45 silly install resolved bl: '^0.7.0',
45 silly install resolved 'through2-map': '^1.2.1',
45 silly install resolved 'colors-tmpl': '^0.1.0',
45 silly install resolved after: '^0.8.1',
45 silly install resolved rimraf: '^2.2.6',
45 silly install resolved chalk: '^0.4.0',
45 silly install resolved through2: '^0.4.1' },
45 silly install resolved bin: { learnyounode: './learnyounode.js' },
45 silly install resolved preferGlobal: true,
45 silly install resolved gitHead: '46361c1b7007d470923e1685b12afbaa574b8cc3',
45 silly install resolved bugs: { url: 'https://github.com/rvagg/learnyounode/issues' },
45 silly install resolved homepage: 'https://github.com/rvagg/learnyounode',
45 silly install resolved _id: '[email protected]',
45 silly install resolved scripts: {},
45 silly install resolved _shasum: 'bea0a40c7e59823f543f5ed2a9bb1c4d6df6eb09',
45 silly install resolved _from: 'learnyounode@*',
45 silly install resolved _npmVersion: '2.1.11',
45 silly install resolved _nodeVersion: '0.10.33',
45 silly install resolved _npmUser: { name: 'rvagg', email: '[email protected]' },
45 silly install resolved maintainers: [ [Object] ],
45 silly install resolved dist:
45 silly install resolved { shasum: 'bea0a40c7e59823f543f5ed2a9bb1c4d6df6eb09',
45 silly install resolved tarball: 'http://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz' },
45 silly install resolved directories: {},
45 silly install resolved _resolved: 'https://registry.npmjs.org/learnyounode/-/learnyounode-1.0.7.tgz' } ]
46 info install [email protected] into /usr/local/lib
47 info installOne [email protected]
48 verbose installOne of learnyounode to /usr/local/lib not in flight; installing
49 verbose lock using /home/yonas/.npm/_locks/learnyounode-3267a39f0ae1a4f5.lock for /usr/local/lib/node_modules/learnyounode
50 silly install write writing learnyounode 1.0.7 to /usr/local/lib/node_modules/learnyounode
51 silly cache addNamed cb [email protected]
52 verbose unbuild lib/node_modules/learnyounode
53 verbose gentlyRm vacuuming /usr/local/lib/node_modules/learnyounode
54 verbose tar unpack /home/yonas/.npm/learnyounode/1.0.7/package.tgz
55 verbose tar unpacking to /usr/local/lib/node_modules/learnyounode
56 verbose gentlyRm vacuuming /usr/local/lib/node_modules/learnyounode
57 silly gunzTarPerm modes [ '775', '664' ]
58 error tar.unpack untar error /home/yonas/.npm/learnyounode/1.0.7/package.tgz
59 verbose unlock done using /home/yonas/.npm/_locks/learnyounode-3267a39f0ae1a4f5.lock for /usr/local/lib/node_modules/learnyounode
60 verbose fstream_path /usr/local/lib/node_modules/learnyounode
61 verbose fstream_type Directory
62 verbose fstream_class DirWriter
63 verbose stack Error: EACCES, mkdir '/usr/local/lib/node_modules'
64 verbose fstream_stack /usr/lib/node_modules/npm/node_modules/fstream/lib/writer.js:171:23
64 verbose fstream_stack /usr/lib/node_modules/npm/node_modules/mkdirp/index.js:46:53
64 verbose fstream_stack Object.oncomplete (fs.js:107:15)
65 verbose cwd /home/yonas
66 error Linux 3.13.0-43-generic
67 error argv "node" "/usr/bin/npm" "install" "learnyounode" "-g"
68 error node v0.10.33
69 error npm v2.1.16
70 error path /usr/local/lib/node_modules
71 error code EACCES
72 error errno 3
73 error Error: EACCES, mkdir '/usr/local/lib/node_modules'
73 error { [Error: EACCES, mkdir '/usr/local/lib/node_modules']
73 error errno: 3,
73 error code: 'EACCES',
73 error path: '/usr/local/lib/node_modules',
73 error fstream_type: 'Directory',
73 error fstream_path: '/usr/local/lib/node_modules/learnyounode',
73 error fstream_class: 'DirWriter',
73 error fstream_stack:
73 error [ '/usr/lib/node_modules/npm/node_modules/fstream/lib/writer.js:171:23',
73 error '/usr/lib/node_modules/npm/node_modules/mkdirp/index.js:46:53',
73 error 'Object.oncomplete (fs.js:107:15)' ] }
74 error Please try running this command again as root/Administrator.
75 verbose exit [ 3, true ]
76 verbose unbuild lib/node_modules/learnyounode
77 verbose gentlyRm vacuuming /usr/local/lib/node_modules/learnyounode

Question on "require"

Hi, I'm a beginner, just a couple of questions :)

When you call require('some_module') in node here is what happens:

  1. if a file called some_module.js exists in the current folder node will load that, otherwise:
  2. node looks in the current folder for a node_modules folder with a some_module folder in it
  3. if it doesn't find it, it will go up one folder and repeat step 2

Isn't it needed to add ./ in require to find local modules? i.e. if I have filter.js in my current directory and I want to load it as a module I would require('./filter').

Also, reading this http://www.bennadel.com/blog/2169-where-does-node-js-and-require-look-for-modules.htm

var utils = require( "utils" );

If the given module is not a core module, Node.js will then begin to search for a directory named, "node_modules". It will start in the current directory (relative to the currently-executing Javascript file in Node) and then work its way up the folder hierarchy, checking each level for a node_modules folder.

Node.js will perform a hierarchical directory search for "node_modules" and "utils" in the following ways:

./node_modules/utils.js
./node_modules/utils/index.js
./node_modules/utils/package.json

I'd like some clarification because the explanation above seems different with yours.
Thank you.

Semicolons (not really an issue)

I was just wondering why you don't use semicolons?
I'm fairly new to node, so thanks a lot for making this book. You do a great job of explaining things in a simple, practical way. Thank you!

Error in solution of My First Async IO lesson

After executing the program successfully with your own code, when user gets the solution and tries it again the it tests fail. So that program which you have provided is not working correctly. Please correct the program.

RequireBin Example Doesn't Work on Chromium

Great article. Wanted to try out the RequireBin example and got the following errors:

(on Webpage)

Bundling error:
Error: CORS request rejected: https://wzrd.in/multi

(in console)
Access to XMLHttpRequest at 'https://wzrd.in/multi' from origin 'http://requirebin.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
bundle.js:4 POST https://wzrd.in/multi net::ERR_FAILED

Here are my browser details:

Chromium | 76.0.3809.100 (Official Build) Built on Ubuntu , running on LinuxMint 18.3 (64-bit)
OS | Linux

parseInt mystery

This isn't so much an issue as I'm just hoping to discuss an example and learn more spells. Hope that is ok here!

I was fiddling with the code example below when I noticed you don't need to call parseInt in order for the ++ operand to work on myNumber. I modified the example with setTimeout to verify this.

As a test I also tried to pass fileContents into console.log(typeof()) from within the addOne function. I expected this would return string but instead it returned object.

My question is twofold. First, why will this example work without parseInt? Second, why does typeof(fileContents); return object instead of string. I'll appreciate any input. Thanks!

var fs = require('fs');
var myNumber = undefined;

function addOne() {
  fs.readFile('number.txt', function doneReading(err, fileContents) {
    /// console.log(typeof(fileContents)); logs an object
    myNumber = parseInt(fileContents);
    /// myNumber = (fileContents); works without parseInt
    myNumber++;
  })
}

addOne();

setTimeout(function() {
    console.log(myNumber); /// logs myNumber++
}, 1000);

Patterns?

In Core Modules you write:

The aforementioned patterns that are used in node core are: callbacks, events, streams and modules.

But it's not clear to me what patterns you are referring to as that is the first instance of the word. Maybe something like:

Node handles I/O with: callbacks, events, streams and modules. If you learn how these four things work then you will be able to go into any module in node core and have a basic understanding about how to interface with it.

Callbacks pseudocode missing passed arguments

In the Callbacks section, the code example it gives seems to be missing some passed arguments. In particular, this code:

var fs = require('fs')
var myNumber = undefined

function addOne(callback) {
  fs.readFile('number.txt', function doneReading(err, fileContents) {
    myNumber = parseInt(fileContents)
    myNumber++
    callback()
  })
}

function logMyNumber() {
  console.log(myNumber)
}

addOne(logMyNumber)

doesn't pass anything to logMyNumber(). I was using this exact code for the async i/o exercise in the learnyounode tutorial, and I was getting a 0 returned. When I changed the code to this:

var fs = require('fs')
var myNumber = undefined

function addOne(callback) {
  fs.readFile('number.txt', function doneReading(err, fileContents) {
    myNumber = parseInt(fileContents)
    myNumber++
    callback(myNumber)
  })
}

function logMyNumber(myNumber) {
  console.log(myNumber)
}

addOne(logMyNumber)

it worked as expected. I'm not sure if I'm missing some JS idiosyncrasy, but this is what worked for me.

Questions

Max Ogden Hello, I'm here for two reasons.
The first is to ask for permission to translate this material into Portuguese of Brazil and the second suggesting the separation of the branches named as follows "version / language" when to change the branch readm.md will be referring to language.

Waiting for your reply. ˆ ˆ

Can we simplify the callbacks pseudocode please?

I feel like this is much more complicated than the concrete callbacks example we've just encountered.

function addOne(thenRunThisFunction) {
  waitAMinute(function waitedAMinute() {
    thenRunThisFunction()
  })
}

addOne(function thisGetsRunAfterAddOneFinishes() {})

Shouldn't thenRunThisFunction() be called one line further down, as shown below, for true reflection of the concrete example before this part?

function addOne(thenRunThisFunction) {
  waitAMinute(function waitedAMinute() {
  })    
  thenRunThisFunction()
}

addOne(function thisGetsRunAfterAddOneFinishes() {})

But then, is waitedAMinute even invoked at any point at all?
This would make much more sense to me:

function addOne(thenRunThisFunction) {
  waitAMinute()
  thenRunThisFunction()
}

addOne(function thisGetsRunAfterAddOneFinishes() {})

Of if the point is that waitAMinute() is async, while waitedAMinute is either sync, or a cb, then can we name them that way? eg. waitAMnuteAsync() and waitedAMinuteCallback()

Time Server Problem

The solution i provide doesn't seem pass the test.

The reason is the wrong use of date functions in the test.

This is the solution you provide:

var net = require('net')

function zeroFill(i) {
   return (i < 10 ? '0' : '') + i
 }

 function now () {
   var d = new Date()
   return d.getFullYear() + '-'
     + zeroFill(d.getMonth()) + '-'
     + zeroFill(d.getDay()) + ' '
     + zeroFill(d.getHours()) + ':'
     + zeroFill(d.getMinutes())
 }

 var server = net.createServer(function (socket) {
   socket.end(now() + '\n')
 })
 server.listen(8000)

d.getMonth return an integer between 0-11 should be normalize
d.getDay return the day of the week, an integer between 0-6, should be used getDate() instead

so the correct solution is:

var net = require('net')

function zeroFill(i) {
  return (i < 10 ? '0' : '') + i
}

function now () {
  var d = new Date()
  return d.getFullYear() + '-'
    + zeroFill(1+d.getMonth()) + '-'
    + zeroFill(d.getDate()) + ' '
    + zeroFill(d.getHours()) + ':'
    + zeroFill(d.getMinutes())
}

var server = net.createServer(function (socket) {
  socket.end(now() + '\n')
})
server.listen(8000)

I don't know how Workshopper does its magic, so my attempt to change the solution.js in problems/time_server folder doesn't work ...

I suppose that it run custom solution and compare result with your solution following the setup rule in setup.js ... but i think it's a little more complicate than that.

Hope it was helpful.
Best Regards
Francesco

Promises add complexity?

This is the first time ever I read that promises "add complexity over callbacks". Every single other post states the opposite:

fs.readFile('movie.mp4')
  .then(function(data) {
    // do stuff with data
  })
  .error(function(error) {
    // handle error
  })

This adds complexity, and not everyone wants that. Instead of two separate functions node just uses a single callback function. Here are the rules:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.