Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Paul Ryan
4,584 PointsProblem displaying JSON
Hey guys,
I am currently doing the Express Basics course and going really well but I can't seem to get my JSON working although I went to the GItHub repo for the course and copy and pasted in the JSON and still no luck.
Here is my app.js
'use strict';//throws an error if there is any typos
var express = require('express'),
posts = require('./mock/posts.json');
//express function() is a top level function created by the module
var app = express();//app is now extended and altered
app.set('view engine', 'jade');//tells express our view engine
app.set('views', __dirname + '/templates');//tells express where our templates are
//create a route
app.get('/', function(req, res){
res.render('index');
});
//route with a parameter
app.get('/blog/:title?', function(req, res){
var title = req.params.title;
if(title == undefined)
{
res.status(503);//503 tells search engines the page is under construction
res.send("This page is under construction");
}
else
{
var post = posts[title];
res.render('post', {post: post});
}
});
//blog test route
app.get('/test', function(req, res){
res.json(posts);
});
//set up dev server
app.listen(3000, function(){
console.log("The frontend server is running on port 3000")
});
Here is my JSON:
{
"I like to run!": {
"title": "I like to run!",
"description": "Fanny pack vinyl put a bird on it, small batch viral migas 8-bit meditation Shoreditch keytar health goth bespoke sustainable. Viral you probably haven't heard of them try-hard ennui, pug Thundercats selfies. Normcore cray health goth, umami ennui beard art party skateboard squid distillery."
},
"Crossfit is Cool": {
"title": "Crossfit is Cool",
"description": "Fanny pack vinyl put a bird on it, small batch viral migas 8-bit meditation Shoreditch keytar health goth bespoke sustainable. Viral you probably haven't heard of them try-hard ennui, pug Thundercats selfies. Normcore cray health goth, umami ennui beard art party skateboard squid distillery."
},
"Swimming is great for the joints": {
"title": "Swimming is great for the joints",
"description": "Fanny pack vinyl put a bird on it, small batch viral migas 8-bit meditation Shoreditch keytar health goth bespoke sustainable. Viral you probably haven't heard of them try-hard ennui, pug Thundercats selfies. Normcore cray health goth, umami ennui beard art party skateboard squid distillery."
}
}
The output of my JSON on the browser is not formatted, it basically just outputs as is so I cannot use parameters to filter the information as it just sees it as a file of text.
Any help would be great.
Thanks.
1 Answer
Jonathon Pemberton
20,090 PointsHey there!
That's actually the expected response. If you use Chrome, you can use an extension called JSONView (located in the Chrome store here: https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?utm_source=chrome-app-launcher-info-dialog)
It helpfully formats your JSON so you can look at what's there.
You can also copy and paste it into something like http://jsonprettyprint.com/ (as suggested by Andrew in a previous course) to get a nicely printed version of your JSON. This is helpful if you don't like using browser plugins and still want to see your JSON formatted nicely.
There are a few other things to mention if you're interested:
In the new version of Node, it's better to include status codes in your responses as well. So, when you send the JSON response, do something like this: res.status(200).json(posts);
I would suggest that you change your conditional in the if statement in the get route to a single post (around line 16) to something like this:
if(typeof title === 'text') {
// success
} else {
// error
}
As a practice, it's better to look for what your code should be rather than what it shouldn't be. This way, you don't get something unexpected like a null value that would pass the condition you currently have.
Hope this helps!
Paul Ryan
4,584 PointsGreat answer, thanks
Chyno Deluxe
16,936 PointsChyno Deluxe
16,936 Points//Fixed code presentation