-1

I've tried this tutorial on how to set up a localhost, and some code worked, some didn't. But currently I am receiving strange experience with it:

Below in the code at res.sendfile it's important to mention that the terminal comments this command as deprecated.

Using res.sendFile gives however the following error.

I've tried to use .sendFile with the absolute path, but it didn't work:

res.sendFile('localhost:3000/client/index.html');
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var port = 3000;

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

I am just trying to make a simple connection between the server.js and the index.html. The expected output is to have index.html displayed instead of the error.

1 Answers1

0

You can use the __dirname variable which stores the absolute path of the current executing script. You can append this path in the sendFile function.

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

Alternatively you can use the Node.js path module for joining paths

const path = require('path');
...
...
res.sendFile(path.join(__dirname,'/client/index.html'));

path.join ensures correct slashes in case of UNIX / Windows systems.

Stephen S
  • 3,936
  • 2
  • 23
  • 33