azat-co / expressworks

Learn Express.js from the author of one of the best books on Express.js—Pro Express.js— with this workshop that will teach you basics of Express.js.
MIT License
709 stars 220 forks source link

Expressworks #8 #70

Closed tvollmer89 closed 9 years ago

tvollmer89 commented 9 years ago

I can't understand why this is not accepting my code, the output is the same. here is my program.js:

var express = require('express')
var app = express()
var fs = require('fs')

app.get('/books', function(req, res){
  var filename = process.argv[3]
  fs.readFile(filename, function(e, data) {
    if (e) return res.send(500)
    try {
      books = JSON.parse(data)
    } catch (e) {
      res.send(500)
    }
    res.json(books)
  })
})

app.listen(process.argv[2])

And this is what the response is to "expressworks verify program.js"

Your submission results compared to the expected:

             ACTUAL                                 EXPECTED                    
──────────────────────────────────────────────     ──────────────────────────────────

   "["                                 !=    "[{\"title\":\"Express.js Guide\",\"tags\":    [\"node.js\",\"express.js\"],\"url\":\"http://expressjsguide.com\"},{\"title\":\"Rapid Prototyping with  JS\",\"tags\":[\"backbone.js\",\"node.js\",\"mongodb\"],\"url\":\"http://rpjs.co\"},{\"title\":\"JavaScript: The  Good Parts\",\"tags\":[\"javascript\"]}]"
   "    \"title\": \"Express.js Guide\"," !=                                       
   "    \"tags\": ["                   !=                                       
   "      \"node.js\","                !=                                       
   "      \"express.js\""              !=                                       
   "    ],"                            !=                                       
   "    \"url\": \"http://expressjsguide.com\"" !=                                       
   "  },"                              !=                                       
   "  {"                               !=                                       
   "    \"title\": \"Rapid Prototyping with JS\"," !=                                        
   "    \"tags\": ["                   !=                                       
   "      \"backbone.js\","            !=                                       
   "      \"node.js\","                !=                                       
   "      \"mongodb\""                 !=                                       
   "    ],"                            !=                                       
   "    \"url\": \"http://rpjs.co\""   !=                                       
   "  },"                              !=                                       
   "  {"                               !=                                       
   "    \"title\": \"JavaScript: The Good Parts\"," !=                                       
   "    \"tags\": ["                   !=                                       
   "      \"javascript\""              !=                                       
   "    ]"                             !=                                       
   "  }"                               !=                                       
   "]"                                 !=                                       

────────────────────────────────────────────────────────────────────────────────

 ✗ Submission results did not match expected!

# FAIL

Your solution to JSON ME didn't pass. Try again!

Please help!

prashcr commented 9 years ago

I'm having the same problem. This is my solution

var express = require('express'),
    fs = require('fs'),
    app = express()
app.get('/books', function(req, res){
  fs.readFile(process.argv[3], function(err, data) {
    if (err) return res.send(500)
    res.json(JSON.parse(data))
  })
}).listen(process.argv[2])

I even tried copying the suggested solution from this repo, and other solutions posted on the web, nothing works

Exact same submission results as mentioned by tvollmer89, with both my own and the suggested solution. Looks like a bug.

meowfcc commented 9 years ago

As a workaround you can add:

app.set('json spaces', 0);

The problems happens, when you use globally installed expressworks (with nvm) and have locally installed express.

Expressworks spawns child processes for the solution and the submission. The solution file is located at

~/.nvm/.../expressworks/exercises/json_me/solution/

So the solution child process looks for node_modules up the directory hierarchy until it reaches

~/.nvm/.../expressworks/node_modules/

where it finds express module of version x.

The submission file is located at

~/workspace/express-tut/

and finds express of version y at

~/workspace/node_modules/

y uses by default json spaces set to 2, while x doesn't have default value (i.e. undefined), because of that output of the submission and solution processes differs, even if the programs are identical.

tvollmer89 commented 9 years ago

Worked! thank you!!!

azat-co commented 9 years ago

@meowfcc :+1:

ZebGirouard commented 8 years ago

@meowfcc , thank you! @azat-co , any way to put a note about this in the exercise? app.set('json spaces', 0); is a non-obvious solution, but way better than the hour of googling "how to strip newline/whitespace from JSON.parse"

azat-co commented 8 years ago

@ZebGirouard no one should ever install express globally (since version 4 at least) so app.set('json spaces', 0) is not the solution. the solution is to npm uninstall -g express adding the wrong solution will only add to the confusion, in my humble opinion

of course, it's good to know about json spaces so I'm not totally against adding it feel free to submit a PR.

ZebGirouard commented 8 years ago

@azat-co , nah, you're probably right. Thanks for listening!