Why not universally route all requests using a one short function?

Viewed 22

I've been learning Node JS + Express and pretty much 100% of the dozens of tutorials I've watched either route each specific browser request one-by-one in a main JS file, or they separate routes into separate files (again, one-by-one in their respective files) and link them all back to the index.js file.

I've been trying to understand why these methods in general would be better than just writing something simple like this which handles all get requests universally by going to the proper file in the directory based off what the url is (and can pull the index.html file from each automatically)? It seems like it would be a lot of never ending code for what could be replaced with:

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname+req.path+'/'));
});

Why are the methods I've seen used as opposed to the method I thought of?

0 Answers
Related