-2

Which of the following method is preferable to render the static assets. Consider that only helpPage.html is the only file exist in the public directory

Method 1:

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

Method2:

app.use((req, res) => {
   res.render(__dirname + '/public/helpPage.html');
})
Prem
  • 5,685
  • 15
  • 52
  • 95

1 Answers1

0

If helpPage.html is the only static file you will deliver, I propose a third option:

app.get('/helpPage.html', (req, res)=>{
    res.sendFile( __dirname + '/public/helpPage.html');
});

I don't see the purpose of using app.use here.

Additionally you may want to use path.join:

const path = require('path');
app.get('/helpPage.html', (req, res)=>{
    res.sendFile( path.join(__dirname, '/public/helpPage.html') );
});

This ensures that the paths are properly joined regardless of what machine you are running on.