GithubHelp home page GithubHelp logo

marcwan / learningnodejs Goto Github PK

View Code? Open in Web Editor NEW
280.0 280.0 244.0 1.89 MB

Source code to accompany my book, "Learning Node.JS"

JavaScript 89.60% PHP 0.03% CSS 0.21% Shell 0.56% HTML 9.49% Batchfile 0.02% Makefile 0.09%

learningnodejs's People

Contributors

antoniom avatar leoorpio avatar marcwan avatar ptdecker avatar tmfelwu 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

learningnodejs's Issues

I'm on Chp. 9, Learning Node.js

Where do you recommend I install "mysql" to?; I'm running Windows. Can I install it anywhere?; Do I need to set an environment variable when I'm finished?

Thank you in advance!

I think I found some errata!

Errata:
err.error in handle_get_album... pg. 75

I can't get the following line to work (it works when I use double quotes) pg. 79:
curl -X GET 'http://localhost:8080/albums/italy2012.json?page=1&page_size=20'
(I had to make a slight modification to "07_get_params.js")
Does marcwan have any idea what it could be?
I also can't get the long curl to run at the top of page 83 (the client side of 08_post_data.js).

Thank you in advance!

send_failure function error in listing 4.2

In the book, the variable 'code' is used twice in the function send_failure.

Here's a fix:

function send_failure(res, web_server_code, err) {
    var code = (err.code) ? err.code : err.name;
    res.writeHead(web_server_code, { "Content-Type" : "application/json" });
    res.end(JSON.stringify({ error: code, message: err.message }) + "\n");
}

Incorrect date on chapter09/01_mysql_demos/01_mysql_testing.js

Incorrect date causes MySQL error

** 2b. create albums.
undefined

** 2c. create albums.
undefined

** 3. Add pictures.
Aw, there was an error:
{ [Error: ER_TRUNCATED_WRONG_VALUE: Incorrect datetime value: '2010/06/12 >08:80:40' for column 'date' at row 1]
code: 'ER_TRUNCATED_WRONG_VALUE',
errno: 1292,
sqlState: '22007',
index: 0 }

This error was caused by the following data.

        { filename: "photo_03.jpg",
          albumid: "japan2010",
          description: "shinjuku is nice",
          date: "2010/06/12 08:80:40" },

code err

On page 63(in Chinese Book) there is a function named "load_album" and a varable is defined as ps. Then ps = only_files.splice(page * page_size, page_size), which I think has something wrong because splice has the structure:arrayObject.splice(index,howmany,item1,.....,itemX), where index means location to delete. Therefore ps= only_files.splice(0, page*page_size).

official forum?

Hi Marc!

Im reading your book..and already I like it more than all the other ones because of the way you dive into the async and show the common patterns right up front with great explaining.

I was wondering tho - is there an official forum site for the book? or are you just using this repo as the forum for the book?

cheers

TJ

connect and express issue

In chapter 8 the example 02_create_album run but the DB is not updating on init. It should be showing a new a PhotoAlbums db but it does not.
Getting this on launching server.js

connect deprecated multipart: use parser (multiparty, busboy, formidable) direct
ly node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:56:20
connect deprecated limit: Restrict request size at location of read node_modules
\express\node_modules\connect\lib\middleware\multipart.js:86:15

Even if I try and force express versions before 4 in which connect is not longer a dependency it still does the same. Not sure if it's express of mongoddb issue?

Is ./ optional?

I'm reading Learning NodeJS... There are 2 distinct fs.readdir examples

Page 96: The path includes ./
Page 73: The path does not specify ./

Thank you in advance!

Still there??

Are you still supporting this book at all? Also, you might check your website... it is down.

Chapter 1 Questions

I ran the debugging.js program and it still prints out 'Thanks for calling!' despite the error in the variable content_length. The value for that in the header is still undefined but the rest of the script works perfectly.

Learning NodeJS, Chapter 8, Page 177

The following 2 lines (page 177):
var db = require('./db.js');
var albums = db.albums;

I see the genius in it... I don't fully grasp it...;

It seems, the 2nd time I request the module, I have access to the collections...;

Chapter 2 nit: in geometry, a square is a rectangle

Maybe this is just me, but the examples used to demonstrate inheritance demonstrated in Chapter 2 are bugging me a little. The code is fine, but in plane geometry, "A square is a degenerate rectangle", but a rectangle is not (necessarily) a square.

So, these examples, while of course they're completely true for this particular code as written, made me a little worried that they might at least momentarily confuse students by contradicting what they've learned in math class:
console.log(sq instanceof Rectangle); // false

This one -- not in the Safari ebook but found here in github in Chapter 2 / classes.js -- seems "worse" because this particular instance of rectangle does have unequal sides:
console.log(re instanceof Square); // true

I do understand that from a computer science perspective, what you've got is perfectly correct. Even if JavaScript used structural typing instead of nominal typing, it would work. (A Square has a width property. A Rectangle has width and height properties. A Square does not have a height property, so it is not a kind of Rectangle; a Rectangle does have a width property and so it is usable as a Square.)

Well, having typed all that, I'm thinking I'll treat this mild cognitive dissonance as a feature rather than a bug and if it even comes up in class tomorrow, use it as an opportunity to discuss how type and inheritance in programming is subtly different from type and kind-of in other contexts.

So, I guess this isn't really much of an issue, but if you wanted to add a footnote to a third edition about type in JavaScript vs. math, it might save a few head scratches here or there. Thanks for listening :-)

code err

On page 63(in Chinese Book) there is a function named "load_album" and a varable is defined as ps. Then ps = only_files.splice(page * page_size, page_size), which I think has something wrong because splice has the structure:arrayObject.splice(index,howmany,item1,.....,itemX), where index means location to delete. Therefore ps= only_files.splice(0, page*page_size).

bad_json() & do_rename()

I am working through your Chapter 4 example => specially writing out the post_data.js file. I noticed that the bad_json() and do_rename() functions are both undefined. So I came here and found that a few years ago someone asked about the bad_json() function (#3). At that point in time you noted that the bad_json() method was defined at the bottom of the 08_post_data.js file. However, I do not see it defined currently, and I also do not see the do_rename() function either. Would you mind writing those into your 08_post_data.js file?

Final chapter 6 examples broken

Within content/album.js in chapter 6 (using the code from your github repository, not the out of date in the book code), you have:

$.getJSON("/albums/" + album_name + ".json", function(d) {
    var photo_d = massage_album(d);
    $.extend(tdata, photo_d);
}

This causes a 'unexpected identifier' error. I believe it is due to 'album_name' never being initialized anywhere.

Any hints on how to resolve this?

getting error in LearningNodeJS/Chapter03/04_async_good.js

fs.js:688
binding.read(fd, buffer, offset, length, position, req);
^

TypeError: fd must be a file descriptor
at TypeError (native)
at Object.fs.read (fs.js:688:11)
at /home/amitauddy/Working/NodeLiveLessons/async.js:5:12
at FSReqWrap.oncomplete (fs.js:123:15)

Node v6.10.1

Cannot display jpeg image through browser or curl

Copied verbatim from book and from git repo - pointing app towards jpg url ends up serving broken jpg. Tried with multiple images.

Browser will not display image, firefox says: "The image “http://localhost:8080/content/example.jpg” cannot be displayed because it contains errors."

Using curl -o test.jpg localhost:8080/content/example.jpg returns a corrupted jpg file, opening the test.jpg with gthumb says: "Error interpreting JPEG image file (Not a JPEG file: starts with 0xef 0xbf)"

What is wrong?

send bad_json() to send_failure?

Hi

I was having trouble with this part working till I just passed it the (e) that was caught...

what you have ( chapter 4 json post )

                } catch (e) {
                    // got a body, but not valid json
                    send_failure(res, 403, bad_json());
                    return;
                }

what I changed it to... not sure why its not working with your original code... but I saw that the send_failure was not liking getting the function bad_json? it was looking for the error?

            } catch (e) {
                // got body but not valid json
                send_failure(res, 403, e);
                return;
            }
...

function send_failure(res, code, err) {
    console.log(code);
    console.log(err);

    var code = (err.code) ? err.code : err.name;
    res.writeHead(code, { "Content-Type" : "application/json" });
    res.end(JSON.stringify({ error: code, message: err.message }) + "\n");
}

I was just logging there to see whats going on.... the rest I think is the same? am i not understanding something?

is the err parameter ok to send bad_json into?

Error: Callback was already called

I am working through Chapter 8, page 177 where I am trying to get the photo album server to startup after implementing the very first steps in MongoDB integration, and I keep getting the same error sequence:

..\Node\Tutorial\photo_album_express\app\node_modules\mongodb\lib\mongo_client.js:350
throw err
^
Error: Callback was already called.
at ..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:840:32
at Db.collection (..\Node\Tutorial\photo_album_express\app\node_modules\mongodb\lib\db.js:457:27)
at async.waterfall.exports.albums (..\Node\Tutorial\photo_album_express\app\data\db.js:33:16)
at nextTask (..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:5070:14)
at ..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:5064:13
at apply (..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:21:25)
at ..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:56:12
at ..\Node\Tutorial\photo_album_express\app\node_modules\async\dist\async.js:843:16
at MongoClient.connect (..\Node\Tutorial\photo_album_express\app\data\db.js:25:17)
at connectCallback (..\Node\Tutorial\photo_album_express\app\node_modules\mongodb\lib\mongo_client.js:428:5)

The "db.js:33:16" is referencing the db.collection("albums", cb); line and the "db.js:25:17" is referencing the cb(null) line within the MongoClient.connect function.

I even tried replacing my data/db.js file with exactly what you provide in repository (https://github.com/marcwan/LearningNodeJS/blob/master/Chapter08/02_create_album/app/data/db.js)

I've searched around but I can't seem to figure out this snag. What is happening?

issue in the example of chapter 6

To make the example in chapter 6 work (to show the images when clicking an album), handle_incoming_request has to have another else if branch, something like this:

else if(core_url.substr(0,8) == '/albums/' && core_url.substr(core_url.length-5) != '.json'){
    serve_static_file('albums/' + core_url.substr(8), res);
}

Otherwise the client can not request something like http://localhost:8080/albums/italy2012/xxx.jpg

Problem in running js file

I just started reading this book..
In part I, Chapter 1, it has been mentioned in the section "Editing and Running JavaScript Files", that we can write JavaScript code into a file using a text editor and then the code is compiled and run via the command line using the node command, What i don't understand is that where do we save the file in the hard disk or how do we access the file via node ( i tried the exercise as it is but only three dots '...' appear and nothing happens ) ??

Pls help.

Chapter 2 - String functions - Page 28

Hi Marc,

I spotted a typo on P28 in the string functions section it says "to extract a substring from a string, use the substr or splice function..."

It should say "...slice function."

Totally minor issue, the code beneath is correct :)

Cheers,

Rob

Unused private variable on album prototype

This is in reference to /Chapter05/04_album_module/album_mgr/lib/album.js.

In that file, a _photos property is added to the album prototype, but it appears to never be used.

The code in the if statement will never be executed because no assignment to the _photos property ever happens, except when it is first declared and assigned a value of null. To be clear, this is the section I am referring to:

if (this._photos != null) {
  callback(null, this._photos);
}

What is the intended use for this property?

Error In Cap 5

This is the code in my example file

function Greeter (lang) {
    this.language = lang;
    this.greet() = function () {
        switch (this.language) {
        case "en": return "Hello!";
        case "de": return "Hallo!";
        case "jp": return "こんにちは!";
        default: return "No speaka that language";
        }
    }
}

If i call this function from my node server

var mm = require('./01_mymodule.js')
mm.create_greeter('en')

I have this error

ReferenceError: Invalid left-hand side in assignment

I chaged with

function Greeter (lang) {
    this.language = lang;
    this.greet = function () {
        switch (this.language) {
        case "en": return "Hello!";
        case "de": return "Hallo!";
        case "jp": return "こんにちは!";
        default: return "No speaka that language";
        }
    }
}

and in my server node

var mm = require('./01_mymodule.js')
mm.create_greeter('en').greet()

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.