Databases Create Entries

Will Albertsen
Will Albertsen
13,095 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
luther wardle
15,875 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

Zimri Leijen
Zimri Leijen
6,955 Points

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) => {