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 Building a MEAN Application Going MEAN with Express Mocking Data

dipeshgautam
dipeshgautam
3,597 Points

Server was running fine until this point but server crashes when I add mock data

I was able to follow allong the videos until now but as soon as I import the mock data as below : var todos = require('../../mock/todos.json'); the server crashes and throws this error :


[nodemon] starting node src/app.js module.js:433 throw err; ^

SyntaxError: /Users/dipeshgautam/Sites/meanproject/mean-todo/mock/todos.json: Unexpected token / at Object.parse (native) at Object.Module._extensions..json (module.js:430:27) at Module.load (module.js:357:32) at Function.Module._load (module.js:314:12) at Module.require (module.js:367:17) at require (internal/module.js:16:19) at Object.<anonymous> (/Users/dipeshgautam/Sites/meanproject/mean-todo/src/api/index.js:8:13) at Module._compile (module.js:413:34) at Object.Module._extensions..js (module.js:422:10) at Module.load (module.js:357:32) [nodemon] app crashed - waiting for file changes before starting...


I checked the relative path for the todos.json file, path seems to be right. If I comment out this line : //var todos = require('../../mock/todos.json');

the server starts running again. What did I do wrong ? Please help I am not able to move forward

4 Answers

It sounds like there's a syntax error in the json fiile. Please post it so we can take a look.

dipeshgautam
dipeshgautam
3,597 Points
//creating mock data
[
    {"name": "Learn JS"},
    {"name": "GO Run 2 miles"},
    {"name": "water plants"},
    {"name": "eat"},
    {"name": "watch TV"},
    {"name": "sleep"}
]

Its very fraustating . I also tried with other json files but I cannot move forward because I cannot import any .json files

dipeshgautam
dipeshgautam
3,597 Points

:) Thank you soooo much ! Now I can go back to completing the course !

Bryant Feld
seal-mask
.a{fill-rule:evenodd;}techdegree
Bryant Feld
Full Stack JavaScript Techdegree Student 26,144 Points

you cannot make a get request for files that are not being served, the express server is set up to serve from "public" so you have to put the mock folder in the public folder and change the path in index.js accordingly, I think this was an uncaught glitch in the course