0

Why do we have to do this for every page in node? For example if someone wants to visit /about we have to do this if someone want to visit /contact we have to do this. Why can't we just create simple HTML pages like about.html and contact.html and serve them when someone visit pages?


app.get("/", (req,res) => {
  res.send("Hello World");
});

app.get("/about", (req, res) => {
  res.send("You are on about page");
});

  • You could. But that is a static way of representing information. this approach is for what you might call a single page app. – JoSSte Jan 22 '19 at 14:30
  • Yes I am talking about static pages only like about us etc. –  Jan 22 '19 at 14:39
  • a benefit of presenting this as a one page app is that all includes are made only once. another is that you have "pretty" urls. if you think it is easier to maintain all includes and so on on several html files, go ahead and do so. and then you can "hack" the `.htaccess` file to redirect `/about` to `/about.html`if you want the pretty urls. this will causee more pain in the long run. – JoSSte Jan 22 '19 at 14:50
  • So if you want to serve static page, instead of use the node js, why don't you configure per exemple a nginx server that will serve static page instead ? if not page is found, you follow the node js after... As explained here : https://stackoverflow.com/questions/29383159/how-do-you-serve-static-files-from-an-nginx-server-acting-as-a-reverse-proxy-for – BENARD Patrick Jan 22 '19 at 18:33

1 Answers1

0

You can, please review Using template engines with Express for more information.

So your example would be:

app.get("/", (req,res) => {
  res.render("index");
});

app.get("/about", (req, res) => {
  res.render("about");
});
Cisco
  • 20,972
  • 5
  • 38
  • 60
  • Still why to use template engine when we can simply use a html page for such static page. Even if that page had some logics involved we could use vanilla JS for that. –  Jan 22 '19 at 14:30