EXPRESS ROUTES CODE CHALLENGES Step 3

In the solution why is the app.get path ‘sausages’ and not 'sausageType ’ which is the name of the array?

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

const PORT = process.env.PORT || 4001;

const sausageTypes = ['bratwurst', 'andouille', 'chorizo', 'boudin', 'kielbasa'];

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

app.listen(PORT, () => {
  console.log(`Server is listening on port ${PORT}`);
});

There is no need for the ‘/sausages’ route to have the same name as one array that is being used in that route .get-method. The .get method simply gets the data from the sausageTypes array and sends it out on the ‘/sausages’ route. As these route methods get more complicated you could be having several arrays and other ways of managing data in one of these route methods.

So essentially could I just do the following:

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

And it would still work? It’s confusing because I did not catch the fact that the path could be anything you wanted it to be. I thought the path was supposed to lead you to the data structure

BTW, the devs hardcoded in the necessity for the pathname to be /sausages, which you can actually see in ‘test.js’ after you ask for the solution.

Yes, route path does not need to be the same as data-structure name. But you need to fetch from the front-end to the path you use in the Express route. So basically use the same route in both front and back-end but I realize that here you are only implementing back-end. It is possible though that for this exercise that specific route (/sausages) was required.

1 Like