Baner Baner - 5 months ago 10
Node.js Question

Using many different html page with express

I am learning node js/express and had a quick question. Is it bad to use mulitple different html files for your get method in express. For example, for each of the get methods, I am opening a different html file.

app.get('/', function(req, res){
var html = fs.readFileSync('index2.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});


app.get('/continuous', function(req, res){
var html = fs.readFileSync('index6.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});

app.get('/output', function(req, res){
var html = fs.readFileSync('index4.html');
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
});

Answer

You can use the res.sendFile to make that process easier:

  • app.get('/game', function (req, res) { res.sendFile('/game.html', { root: path.join(__dirname, '/public') }); });