1

I am trying to load HTML files in nodejs. In public folder, I have two html files test.html and index.html. I would like to use nodejs to showcase the pages, but its working for index.html and for the case of test, it shows an error saying that res.sendFile is not a function.

var express = require('express');
var app = express();
var path = require('path');

// viewed at http://localhost:8080 
app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/index.html'));
});
// viewed at http://localhost:8080 
app.get('/test', function(req, res) {
    res.sendFile(path.join(__dirname + '/public/test.html'));
});

app.listen(8080);
mscdex
  • 104,356
  • 15
  • 192
  • 153
user3001937
  • 2,005
  • 4
  • 19
  • 23

1 Answers1

0

For serving static files such as JS, CSS, images, html files, you can use the express.static built-in middleware function in Express. Serving static files in Express

Say if your 'Public' folder has some JS, html files which is required at client side. Then you need do to go like this :-

var express = require('express');
var app = express();

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

var server = app.listen(8080);
satyam kumar
  • 1,467
  • 13
  • 7