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

Jordan Stokes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Jordan Stokes
Full Stack JavaScript Techdegree Graduate 27,292 Points

Isn't this a better way of coding this?

I feel like this is a much simpler and more DRY way of writing this. Is this wrong? There is only one callback function, and I reuse the 404 error handling in app.js.

router.post("/:qId/answers/:aId/vote-:direction", (request, response, next) => 
{
    const direction = request.params.direction;

    if(direction === 'up' || direction === 'down')
    {
        response.json({
            response: `You sent a POST request in /vote-${direction}`,
            questionId: request.params.qId,
            answerId: request.params.aId,
            vote: direction
        });
    }
    else
    {
        next();
    }
});