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 Build a REST API With Express Building API Routes in Express Building the Answer Routes

How is the API already mounted to questions

When we are creating the post for creating answers we use the following code:

//POST /questions/:id/answers //route for creating answers router.post("/:id/answers", function(req, res){

res.json({ response: "you sent me a post request", body: req.body

}); });

I understand that we have already mounted to questions but I don't understand how.

Should we not have to do: router.post("questions/:id/answers", function(req, res){

1 Answer

In app.js there is the following app.use() statement:

app.use("/questions", routes);

This tells Express to send all requests that begin with /questions to the routes.js file (routes is a variable that loaded the routes.js file at the top of the app.js file).

From there, all further routing will be relative to the /questions route.