How to connect to a Node.js server that's using Express using an HTML file?

You run a node.js express server on LocalHost or a service provider (Heroku, AWS). How do I connect this to my website?

E.G — You go to.. -> Connects to HEROKU server -> HEROKU server sends html file


const http      = require('http'),
      path      = require('path'),
      express   = require('express'),
      app       = express(),
      serv      = require('http').Server(app);

var htmlPath = path.join(__dirname, 'client');


app.get('/', (req, res) => {
    res.sendFile(htmlPath + '/index.html');

var server = serv.listen(2000, () => {
    var host = 'localhost';
    var port = server.address().port;
    console.log(`listening on http://${host}:${port}/`);

In this case, now instead of going to localhost:2000 directly to receive the index.html file, you go to and the server sends the index.html file

91 thoughts on “How to connect to a Node.js server that's using Express using an HTML file?”

  1. Good website! I really love how it is simple on my eyes and the data are well written. I’m wondering how I could be notified whenever a new post has been made. I have subscribed to your RSS which must do the trick! Have a nice day!

  2. Your writing is perfect and complete. safetoto However, I think it will be more wonderful if your post includes additional topics that I am thinking of. I have a lot of posts on my site similar to your topic. Would you like to visit once?


Leave a Comment