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

JSON ME Unable to produce answer #85

Closed su6a12 closed 8 years ago

su6a12 commented 8 years ago

My answer below is almost identical to the official answer and produces the same output, which is not "correct" in that it spans multiple lines and the correct output is all one line. I don't see anything wrong with my code.

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

app.get('/books', function(request, response) { fs.readFile(process.argv[3], function(error, data) { if (error) { console.log(error); } response.json(JSON.parse(data)); }); }); app.listen(process.argv[2]);

samscripts commented 8 years ago

Hi try to use response.send(JSON.stringify(JSON.parse(data)));

check for brackets... Cheers, Sam

su6a12 commented 8 years ago

@samscripts Thanks, it works with response.send(JSON.stringify(JSON.parse(data.toString())));