Issue with Express and sub directories

#1

When I use the following code:

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

TypeError: path must be absolute or specify root to res.sendFile

It errors out due to not being able to load the html file. What is the correct way to call this within the app? When I use this on a normal node server on a Linux machine it works fine.

Thanks!

1 Like

#2

Hi Ninjacoder :wave:

In which directory index.html is located? If the file is in the same directory where you’re launching the express app could you try with '.index.html'?

0 Likes

#3

The file is in a sub directory called public, however I have tested moving it to the same directory. I tried your .index.html and it also did not work, same error.

To give additional info, I tried serving a folder as a static folder for the web content using express which did not work, so I tried simply sending the html file in the same directory, which also did not work.

2 Likes

#4

I am having exactly the same issue, tried various options, same as OP obviously, then as suggested, then with __dirname, and with root specification - always the same error occurs.

Some solution or a walk around would be greatly appreciated.

Cheers!

0 Likes

#5

I’ve resolved my issue in a slightly perverted way, simply specified root directory as a VERY full route.

This is how my res.sendFile looks now:

app.get('/', function(req, res){
res.sendFile('index.html', { root: '/private/var/mobile/Containers/Data/Application/54E4727E-D452-4165-827E-921E9D8D7F7C/Documents/apps/BITCOIN-TICKER3C6F52E5-6C0F-42CA-8599-8BDB0D95BB88/'});
});

I took the whole path for use from the error message in Safari, when I was trying to run index.html on localhost. The path was pointing all the way to Express in my case, name of the app itself is ‘Bitcoin-Ticker’ the rest has been added by play.js, not myself.

Here’s a screenshot, which led me to it:

Can’t paste an image of successful outcome, since I am a new user and can only post one image. Anyways, just take my work for it - it works.

Happy to have play.js, great tool!

0 Likes

#6

I’ve experienced the same issue and the work around posted above doesn’t seem to woerk for me

0 Likes

#7

I’ve done some testing and it seems the issue is in the fs module,
Here’s some code I rote to test it:

const fs = require (“fs”);

const http = require (“http”);

const Path = require (“path”);

let server = http. createServer (function (req, res){

let root = “.”;

let html = “<div>”;

let loop = function (path){

let html = “{”;

let files = fs. readdirSync (path);

!Array. isArray (files) || files. forEach (function (file){

try {

html += “<div>”+ file+ “:<div>” + loop (Path. join (path, file)) +",</div></div>";

}

catch (e){

console. log (e);

}

});

html +="}";

return html;

}

res.writeHead(200, {‘Content-Type’: ‘text/html’});

res. write (loop ("."));

res. end ();

}). listen (3000);

Heres what I get in the console when running and going to localhost in safari:

{ Error: ENOTDIR: not a directory, scandir '.Trashes'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'ENOTDIR: not a directory, scandir \'.Trashes\'',
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'scandir',
  path: '.Trashes' }
{ Error: EPERM: operation not permitted, scandir '.ba'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'.ba\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: '.ba' }
{ Error: ENOTDIR: not a directory, scandir '.file'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'ENOTDIR: not a directory, scandir \'.file\'',
  errno: -20,
  code: 'ENOTDIR',
  syscall: 'scandir',
  path: '.file' }
{ Error: EPERM: operation not permitted, scandir '.mb'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'.mb\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: '.mb' }
{ Error: EPERM: operation not permitted, scandir 'Applications'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'Applications\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'Applications' }
{ Error: EPERM: operation not permitted, scandir 'Library'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'Library\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'Library' }
{ Error: EPERM: operation not permitted, scandir 'System'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'System\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'System' }
{ Error: EPERM: operation not permitted, scandir 'bin'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'bin\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'bin' }
{ Error: EPERM: operation not permitted, scandir 'cores'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'cores\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'cores' }
{ Error: EPERM: operation not permitted, scandir 'dev'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'dev\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'dev' }
{ Error: EPERM: operation not permitted, scandir 'etc'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'etc\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'etc' }
{ Error: EPERM: operation not permitted, scandir 'private'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'private\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'private' }
{ Error: EPERM: operation not permitted, scandir 'sbin'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'sbin\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'sbin' }
{ Error: EPERM: operation not permitted, scandir 'tmp'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'tmp\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'tmp' }
{ Error: EPERM: operation not permitted, scandir 'usr'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'usr\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'usr' }
{ Error: EPERM: operation not permitted, scandir 'var'
   at fs.readdirSync (fs.js:911:3)
   at loop (repl:9:1)
   at Anonymous function (repl:12:1)
   at Array.prototype.forEach (native code)
   at loop (repl:10:1)
   at Anonymous function (repl:22:1)
   at emitTwo (events.js:125:5)
   at emit (events.js:213:7)
   at parserOnIncoming (_http_server.js:602:5)
   at parserOnHeadersComplete (_http_common.js:116:5)
  message: 'EPERM: operation not permitted, scandir \'var\'',
  errno: -1,
  code: 'EPERM',
  syscall: 'scandir',
  path: 'var' }

0 Likes

#9

Hi all for your patience,

Next version fixes these issues. Please report here if you find any other problems.

Thanks for reporting :slight_smile:

0 Likes