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 trialPeter Lu
10,612 PointsWhy is app.listen at the end of the file, and app.get above it? Does the order matter?
Help!
Akash Sharma
Full Stack JavaScript Techdegree Student 14,147 PointsWell, the get methods are run after you initialize the server with the command nodemon, so
app.listen(3000, () =>
console.log('Application is listening on localhost:3000')
);
is executed first but the the app.get methods are executed after server is started and thus it can be placed before.
I did realize that express does not hoist variable names as you need to put
const app = express();
above before invoking functions on it.
Charlie Gallentine
12,092 PointsCharlie Gallentine
12,092 PointsFrom my understanding app.listen() just sets up a server for the app to run on so that you can view it using localhost:[port].
app.get() sets up routes which the app uses to view different sections by sending GET requests to the server.
I'm not sure, but other than convention, it may be last so that the rest of the app is loaded before it makes itself viewable. Having app.listen() starts listening after the applicable routes have been called earlier in app.js.