express js send file code example

Example 1: send html file express

res.sendFile(path.join(__dirname + '/index.html'));

Example 2: express send html file

app.get('/test', function(req, res) {
    res.sendFile('test.html', {root: __dirname })
});

Example 3: sendfile express syntax

app.get('/', function(req, res){
  res.sendFile('index.html');
});

Example 4: express render

// send the rendered view to the client
res.render('index')

// if a callback is specified, the rendered HTML string has to be sent explicitly
res.render('index', function (err, html) {
  res.send(html)
})

// pass a local variable to the view
res.render('user', { name: 'Tobi' }, function (err, html) {
  // ...
})

Tags:

Html Example