Boss Machine Project Tests not working at all!

Hi everybody!

I am on the Boss Machine Project in the Create a Back-End App with JavaScript path:
Boss Machine and I cannot solve the errors. Here are some screenshots & code:

code:
Server.js:

const express = require('express');
const app = express();

module.exports = app;

/* Do not change the following line! It is required for testing and allowing
*  the frontend application to interact as planned with the api server
*/
const PORT = process.env.PORT || 4001;

// Add middleware for handling CORS requests from index.html
const cors = require('cors');
app.use(cors());
// Add middware for parsing request bodies here:
const bodyParser = require('body-parser');
app.use(bodyParser.json());

// Mount your existing apiRouter below at the '/api' path.
const apiRouter = require('./server/api');
app.use('/api', apiRouter)

// This conditional is here for testing purposes:
if (!module.parent) { 
  // Add your code to start the server listening at PORT below:
  app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
  });
};

minionsRouter.js(in the server/ folder) (this is basically the codecademy solution):

const minionsRouter = require('express').Router();

module.exports = minionsRouter;

const { 
  addToDatabase,
  getAllFromDatabase,
  getFromDatabaseById,
  updateInstanceInDatabase,
  deleteFromDatabasebyId,
} = require('./db');

minionsRouter.param('minionId', (req, res, next, id) => {
  const minion = getFromDatabaseById('minions', id);
  if (minion) {
    req.minion = minion;
    next();
  } else {
    res.status(404).send();
  }
});

minionsRouter.get('/', (req, res, next) => {
  res.send(getAllFromDatabase('minions'));
});

Screenshot of testing suite:

Thanks to everybody!

Hiya, your issue is that you haven’t yet mounted your minionsRouter to a path, so that’s why your get requests are not being sent to the proper path.

You need to include this in api.js:

const minionsRouter = require('./minionsRouter'); 
apiRouter.use('/minions', minionsRouter); 

This tells out apiRouter to use the minionsRouter routers for requests that come through to api/minions.

For the route to get a single minion depending on its ID, this is another get request.

This was how I solved it:

minionsRouter.get('/:minionId', (req, res, next) => {
// Get a single minion based on the minionId that's passed in to the getFromDatabase function
   const getMinion = getFromDatabaseById('minions', req.params.minionId); 
//If getFromDatabase returns false, ie if there's an invalid id, send a 404 response 
   if (!getMinion) {
    res.status(404).send("Not found");
   }
// Otherwise, send back the minion that was requested 
   res.status(200).send(getMinion); 
   
});

I hope that helps!

It helps. Thank you @callmej9

1 Like