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 trialSergio Cruz
15,550 PointsWhy doesnt Huston defines post as a variable when requiring the file?
When I do it like that the console throws an error. It only works when I put var before it. However when I launch localhost:3000/blog my file isn't served in json format. It's written as a string, whats going on?
3 Answers
Huston Hedinger
Treehouse Guest TeacherHey there! Example code would be super helpful in both questions and answers.
Catherine's answer is correct. Variables can be defined without the var
keyword when multiple variable declarations are separated by commas.
For example:
'use strict';
var express = require('express'),
posts = require('./mock/posts.json');
Now the reason an error is being thrown if you DO NOT use the var
keyword, is because of the 'use strict';
statement at the beginning of the file. Feel free to check out my workshop on use strict to see why this is a really good idea!
Catherine Gracey
11,521 PointsHe does define it as a variable, it's just easy to miss because he places a comma and starts a new line after declaring the previous variable.
Jason Martin
Courses Plus Student 9,316 Pointswhat about the second part of the question? when we follow along, it comes out as an ugly string... How do you get yours looking good?
{"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."}}
Fredrik Olsson
3,341 PointsMaybe you don't have JSONview added as a plugin in chrome? without it it will look like a long string.
Javier Alvarado
16,060 PointsJavier Alvarado
16,060 PointsDoes the comma trick still work with
const
andlet
?