0

the hierarchy of my files looks something like:

BACKEND(COMPLETE)
->Routers
   ->userrouter.js
->login.html

I wanted the access of my login.html file in userrouter.js for which I copied the Path(absolute) of the login.html file.

But I am getting this error:

path must be absolute or specify root to res.sendFile()

My Code:

function loginUser(req,res)
{
res.sendFile('C:\Users\ASUS\Desktop\backend(complete)\login.html');
res.end();
}
Vatsal A Mehta
  • 392
  • 2
  • 15
  • C:\Users\ backslash in Javascript is an escape sequence, you need to do C:\\Users\\.. But I believe node will also automatically convert forward slashes for you so, C:/users/ will likely work too. – Keith Nov 30 '21 at 12:55
  • @Keith I replaced all backslashes with forwardslashes,but it didnt work. – Vatsal A Mehta Nov 30 '21 at 13:04

2 Answers2

1

Try the Npm package path and use path.join(["yourpath", "here") for cross system compatibility. Note that every part between slashes needs to be its own entry in the array.

damnedOperator
  • 208
  • 2
  • 13
1

Try modifying C:\Users\ASUS\Desktop\backend(complete)\login.html to C:/\Users/\ASUS/\Desktop/\backend(complete)/\login.html

Try using path (path.join) npm package which Node provides out of the box to avoid this confusion irrespective of the OS.

Elikill58
  • 4,050
  • 24
  • 23
  • 45
hitesh v
  • 26
  • 4