FAQ: Learn Express Routers - Refactoring Expressions Routes

This community-built FAQ covers the “Refactoring Expressions Routes” exercise from the lesson “Learn Express Routers”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn Express

FAQs on the exercise Refactoring Expressions Routes

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I’m frustrated beyond belief… this is my code:
const express = require(‘express’);

const { getElementById, getIndexById, updateElement,

seedElements, createElement } = require(’./utils’);

let animals = ;

seedElements(animals, ‘animals’);

animalsRouter = express.Router();

// Get all animals

animalsRouter.get(’/’, (req, res, next) => {

res.send(animals);

});

// Get a single animal

animalsRouter.get(’/:id’, (req, res, next) => {

const animal = getElementById(req.params.id, animals);

if (animal) {

res.send(animal);

} else {

res.status(404).send();

}

});

// Create an animal

animalsRouter.post(’/’, (req, res, next) => {

const receivedAnimal = createElement(‘animals’, req.query);

if (receivedAnimal) {

animals.push(receivedAnimal);

res.status(201).send(receivedAnimal);

} else {

res.status(400).send();

}

});

// Update an animal

animalsRouter.put(’/:id’, (req, res, next) => {

const animalIndex = getIndexById(req.params.id, animals);

if (animalIndex !== -1) {

updateElement(req.params.id, req.query, animals);

res.send(animals[animalIndex]);

} else {

res.status(404).send();

}

});

// Delete a single animal

animalsRouter.delete(’/:id’, (req, res, next) => {

const animalIndex = getIndexById(req.params.id, animals);

if (animalIndex !== -1) {

animals.splice(animalIndex, 1);

res.status(204).send();

} else {

res.status(404).send();

}

});

module.exports = animalsRouter;

it gives me an error, I compare my code to the expressions.js file, looks right, click the solution button, compare the code to the solution exactly the same from what i can tell, reset the exercise and paste my code back in, I get a DIFFERENT error, and I did not change my code from the last. The solution :
const express = require(‘express’);

const { getElementById, getIndexById, updateElement,

seedElements, createElement } = require(’./utils’);

let animals = ;

seedElements(animals, ‘animals’);

animalsRouter = express.Router();

// Get all animals

animalsRouter.get(’/’, (req, res, next) => {

res.send(animals);

});

// Get a single animal

animalsRouter.get(’/:id’, (req, res, next) => {

const animal = getElementById(req.params.id, animals);

if (animal) {

res.send(animal);

} else {

res.status(404).send();

}

});

// Create an animal

animalsRouter.post(’/’, (req, res, next) => {

const receivedAnimal = createElement(‘animals’, req.query);

if (receivedAnimal) {

animals.push(receivedAnimal);

res.status(201).send(receivedAnimal);

} else {

res.status(400).send();

}

});

// Update an animal

animalsRouter.put(’/:id’, (req, res, next) => {

const animalIndex = getIndexById(req.params.id, animals);

if (animalIndex !== -1) {

updateElement(req.params.id, req.query, animals);

res.send(animals[animalIndex]);

} else {

res.status(404).send();

}

});

// Delete a single animal

animalsRouter.delete(’/:id’, (req, res, next) => {

const animalIndex = getIndexById(req.params.id, animals);

if (animalIndex !== -1) {

animals.splice(animalIndex, 1);

res.status(204).send();

} else {

res.status(404).send();

}

});

module.exports = animalsRouter;

Did I find a bug or am I missing something here?

1 Like

Had similar issue… pretty sure I did this right, but couldn’t move on without using default solution. Very frustrating. Oh well, just accept and move on,

Oh look, another Codecademy lesson that is obtuse, poorly (or not at all) explained, and obfuscates would should be a fairly straightforward task with pointless abstraction. It’s been almost three years, judging from the first comment, of this being essentially useless.

1 Like