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

Databases Create Entries

Will Albertsen
Will Albertsen
13,155 Points

How does the home page route automatically go to '/articles'?

I've been studying the code in the supplied files for this project and the one thing I am having a hard time grasping is how the home route automatically goes to '/articles'. I know it has something to do with the index.js file, but it's just not clicking in my brain. In the index.js, a get method is called on the home route '/' with a redirect a '/articles' route. I just don't understand how it can redirect to '/articles' when there is not a get method anywhere with '/articles' as a parameter. What role does the app.use('/articles", articles) in app.js play? Could someone help me make the connection here? Thank you :)

luther wardle
seal-mask
.a{fill-rule:evenodd;}techdegree
luther wardle
Full Stack JavaScript Techdegree Student 18,029 Points

I believe this is because index.js is the initial target for all the routes in this application. If you look in app.js you'll notice

const routes = require('./routes/index');    // so index is where we look initially for where we should route the url

since routes is used like so in app.js:

app.use('/', routes);

the index.js route is used first and this generates the homepage you see as well as sets the route to /articles

1 Answer

Because of this line in app.js

app.use('/articles', articles);

along with

const articles = require('./routes/articles');

whenever a route starts with /articles it looks in the articles file.

It's like, "whenever the link starts with /articles see this file for directions."

Edit // I misinterpret the question.

if you go to localhost:3000 without any parameters you will redirect to /articles because of this line:

router.get('/', (req, res, next) => {
  res.redirect("/articles")
});