Cannot Get /

Im doing the beatbox challenge:
https://www.codecademy.com/paths/create-a-back-end-app-with-javascript/tracks/bapi-javascript-arrays-loops-and-iterators/modules/project-beat-mix/informationals/bapi-p2-beat-mix

When i run npm start server it starts all right on port 4001. When i go to 127.0.0.1:4001 all i get is “Cannot Get /”. Im not sure if this is part of the challenge? but i cant test the code ive written till i fix this.

iv tried with chromium:
Version 76.0.3809.100 (Official Build) Built on Ubuntu , running on LinuxMint 18.3 (64-bit)

and firefox: 68.0.2

any help appreciated!!

What happens when you open http://127.0.0.1:4001/presets? You should get some output.


In the server.js file you can find these routes:

app.get('/presets', (req, res, next) => {
  res.send(presets);
});

// Handle presets requests
app.use('/presets/:id', (req, res, next) => {
  let index = Number(req.params.id);
  let presetArray = req.body;
  let isValidPreset = presetArray.length === 4
  && presetArray.every((singleRow) => {
    return singleRow.length === 16
    && singleRow.every((singleGridElement) => {
      return singleGridElement === true || singleGridElement === false;
    });
  });
  if (app.method === 'PUT' && !isValidPreset) {
    res.status(400).send('Bad Request, send a preset array!');
  } else {
    let method = req.method;
    let [status, preset] = presetHandler(method, index, presetArray);
    res.status(status).send(preset);
  }
});

As you can see, there are no routes definition for app.get('/', ..., that is why server responded with Cannot Get / error.

3 Likes

Yeah ‘http://127.0.0.1:4001/presets’ gave me an array of arrays containing false… So i need to set “apt.get(’/’,” to point to index.html??

Hi,

Digging up this old thread as it’s a top result on google when trying to find help!

To solve this I moved index.html into the public folder and also edited the urls inside index.html to remove the ‘public/’ as it was then creating a double reference.

The original code did work in VSCode live server, so I am guessing there is some automatic return of index.html that is happening there, while running is with nodejs can’t do that?

Curious to know an answer from someone more qualified than I!

Leo

Just an update now I have done some more work on this!

I was mistakenly assuming that the server would be set up to serve content as well as handle the back end - but it is not! You need to simply open index.html locally using your browser, and it will work.

Same applies to lots (all?) of the projects on this pathway.

2 Likes

You beautiful human! Thank you! I was close to going insane checking what I did wrong or if Codecademy provided buggy code. But as it always happens for me with coding is that the answer always seems to be a lot simpler than I think it is.