scotch-io / mean-machine-code

Code samples for the book: MEAN Machine
https://leanpub.com/mean-machine
MIT License
457 stars 227 forks source link

In User CRM chapter, catchall route in server.js is catching requests for js files in index.html #34

Closed Zelbinian closed 9 years ago

Zelbinian commented 9 years ago

Took me a while to track down the source of this issue, but now that I have I don't know how to continue with the rest of the book.

In the server.js file, as it's constructed in previous chapters, we're instructed to write this catchall route for front-end routing just before we start the app listening:

app.get('*', function (req, res) {
    res.sendFile(path.join(__dirname + '/public/app/views/index.html'));
});

And in the index.html file, we're instructed to call our angular services, controllers, and app files thusly:

<!-- controllers -->
<script src="app/controllers/mainCtrl.js"></script>
<script src="app/controllers/userCtrl.js"></script>

<!-- services -->
<script src="app/services/authService.js"></script>
<script src="app/services/userService.js"></script>

<!-- main Angular app files -->
<script src="app/app.routes.js"></script>
<script src="app/app.js"></script>

Because of the catchall route node intercepts these requests and sends back index.html, like we told it to. But we need that catchall route in there to forward requests to the frontend so... what to do isn't clear.

But I could also be missing something.

Zelbinian commented 9 years ago

I figured it out by playing with the routing-app files to see why that app was loading but this one wasn't. Turns out, when I was moving all the stuff from server.js to api.js, this line accidentally got deleted:

app.use(express.static(__dirname + '/public'));

Had no idea that line was so important!