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

JavaScript Express Basics (2015) Developing Express Apps Like a Boss Adding Routes to the App

Sergio Cruz
Sergio Cruz
15,550 Points

Why 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
STAFF
Huston Hedinger
Treehouse Guest Teacher

Hey 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!

Javier Alvarado
Javier Alvarado
16,060 Points

Does the comma trick still work with const and let?

Catherine Gracey
Catherine Gracey
11,521 Points

He 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
PLUS
Jason Martin
Courses Plus Student 9,316 Points

what 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
Fredrik Olsson
3,341 Points

Maybe you don't have JSONview added as a plugin in chrome? without it it will look like a long string.