Why is the route method used?

Could somebody provide a little explanation why does provided solution from the challenge page employs route method of express instance?

https://www.freecodecamp.org/news/search/?query=express%20route seems not to address .route method.

PS Sorry, if it should be a separate topic – please rearrange as better.

I don’t really see a need for it so it might just be personal preference. You will have to ask the person that made the challenge and coded the example.

I didn’t look thoroughly but I’m not sure where in the curriculum the method is even taught.


One thing you will see the app.route() method used for is chaining HTTP methods on the same path.

expressjs docs: app.route()

app.route('/book')
  .get((req, res) => {
    res.send('Get a random book')
  })
  .post((req, res) => {
    res.send('Add a book')
  })
  .put((req, res) => {
    res.send('Update the book')
  })
1 Like

Hello there,

No specific reason that I can remember :sweat_smile:

I often find that I use both app.route(path).<method>(cb) and app.<method>(path, cb). Usually, for no rhyme or reason.

1 Like