where and how to serve files from , in node.js

I am new to node.js/express.js and I am reading some tutorials. I am confused because I am used to the simple apache logic, node.js/express.js logic confuses me. Please help me.

This tutorial uses the default express routes to add/get data from a db. But, at the begging , at the part named "PART 2 – OK, FINE, LET'S DO "HELLO, WORLD!" edits the ...\ROUTES\INDEX.JS file to add just a simple html page. Why is that?

  1. Can I just use the public folder to serve my files and access the by using the same URL?

  2. If I have like 50 files, I have to add 50 similar functions to my ...\ROUTES\INDEX.JS so I can serve them ? Even the simplest static files ?

  3. Can I just put all my files in the public folder and then edit app.js and ...\ROUTES\INDEX.JS ?

Also I was reading the first chapter of the book Jump Start Node.js by Don Nguyen. It does not edit routes, just adds methods to the app.js and implements new modules (named db and user) for adding users to the db. This also adds a new get function to app.js for a simple form.html file.

  1. Again, why can I use the public folder and then edit the app.js and create my own modules?

  2. Again, If I have like 50 files, I have to add 50 similar functions to my app.js so I can serve them ? Even the simplest static files ?

    Finally,

  3. What is the difference between the two methods ? In which case I use them ?

  4. Which one is the best practice ?

Thank you very much

To serve the folder named "public" as static files:

app.use(express.static(__dirname + '/public'));

The reason the tutorial did not put their 'simple' index page into public, is that their 'simple' page is not static. They pass in the data { title: 'Express' } to the dynamic page.

If the title 'Express' is always going to be static, then yes you can serve it from public. However for the sake of the tutorial, we assume they might dynamically change the title from 'Express' to something else.