I’m working on the Boss Machine.
So far this is what I have:
server.js
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser');
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
app.use(cors());
// Add middware for parsing request bodies here:
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 is listening on port ${PORT}`);
});
}
server/api.js
const express = require('express');
const apiRouter = express.Router();
const minions = require('./minions')
apiRouter.use('/minions',minions);
module.exports = apiRouter;
server/minions.js
const express = require('express');
const db = require('./db');
const minionsRouter = express.Router();
//get all minions
minionsRouter.get('/', (req,res) => {
const allMinions = db.getAllFromDatabase('minions');
res.status(200).send(allMinions);
});
//create a minion
minionsRouter.post('/', (req,res) => {
const newMinion = db.addToDatabase('minions', req.body);
res.status(201).send(newMinion);
});
//get one minion
//update a minion (put)
//delete a minion
module.exports = minionsRouter;
When I want to check wether anything is actually working, I type npm run test in the root directory of the project. This is what I get:
npm run test
build-apis-boss-machine@1.0.0 test C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start
cross-env PORT=8000 mocha --watch
/api/minions routes
GET /api/minions
1) returns an array
2) returns an array of all minions
GET /minions/:minionId
3) returns a single minion object
4) returns a full minion object
5) returned minion has the correct id
6) called with a non-numeric ID returns a 404 error
7) called with an invalid ID returns a 404 error
PUT /api/minions/:minionId
8) updates the correct minion and returns it
9) updates the correct minion and persists to the database
10) called with a non-numeric ID returns a 404 error
11) called with an invalid ID returns a 404 error
12) called with an invalid ID does not change the database array
POST /api/minions
13) should add a new minion if all supplied information is correct
DELETE /api/minions
14) deletes the correct minion by id
15) called with a non-numeric ID returns a 404 error
16) called with an invalid ID returns a 404 error
(etc. this describes what is being tested)
0 passing (1m)
18 pending
43 failing
-
/api/minions routes
GET /api/minions
returns an array:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) -
/api/minions routes
GET /api/minions
returns an array of all minions:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) -
/api/minions routes
GET /minions/:minionId
returns a single minion object:
Error: Timeout of 2000ms exceeded. For async tests and hooks, ensure “done()” is called; if returning a Promise, ensure it resolves. (C:\Users\username\documents\projects\codeacademy\project-4-boss-machine-start\test\test.js)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7)
(etc)
I get the same Error for all the tests: a timeout. What is happening here and where can I fix it?