0

I tried following this stackoverflow post to try and see my folder on my static webpage but no luck:https://stackoverflow.com/a/31274417

Here is my code:

const express = require('express');
const app = express();
const db = require('./persistence');
var fs = require('fs');
var files = fs.readdirSync('./static/reports');


app.use(require('body-parser').json());
app.use(express.static(__dirname + '/static'));



db.init().then(() => {
    app.listen(3000, () => console.log('Listening on port 3000'));
}).catch((err) => {
    console.error(err);
    process.exit(1);
});

const gracefulShutdown = () => {
    db.teardown()
        .catch(() => {})
        .then(() => process.exit());
};

process.on('SIGINT', gracefulShutdown);
process.on('SIGTERM', gracefulShutdown);
process.on('SIGUSR2', gracefulShutdown); // Sent by nodemon

Here is a picture of what it looks like from vscode: enter image description here

Here is a picture of the error from docker: enter image description here

Here is the error from text:

internal/fs/utils.js:269

    throw err;

    ^


Error: ENOENT: no such file or directory, scandir './static/reports'

    at Object.readdirSync (fs.js:955:3)

    at Object.<anonymous> (/app/src/index.js:5:16)

    at Module._compile (internal/modules/cjs/loader.js:999:30)

    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

    at Module.load (internal/modules/cjs/loader.js:863:32)

    at Function.Module._load (internal/modules/cjs/loader.js:708:14)

    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)

    at internal/main/run_main_module.js:17:47 {

  errno: -2,

  syscall: 'scandir',

  code: 'ENOENT',

  path: './static/reports'

}

1 Answers1

1

Starting a file path with a / denotes that it's at the root of the filesystem. Don't use a /; instead, just use static/reports. You can also use ./static/reports if the code will be run from the src/ directory every time.

var fs = require('fs');
var files = fs.readdirSync('static/reports/');
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
  • That makes a lot of sense because of the `/` is root in linux so that makes logical sense. HOWEVER, that didn't work for me. I think that is my fault as I didn't put all the code into the post, I just put in the picture. I added the full code. I think it has something to do with `app.use(express.static(__dirname + '/static'));` but I can't tell. I apologize as this is my first time using node.js and I am using the getting started image from docker to learn docker. I updated my post to put in the full code. –  Oct 24 '21 at 02:42
  • @SOUser418 Shouldn't `var files = fs.readdirSync('reports/');` be `var files = fs.readdirSync('static/reports/');`? – shreyasm-dev Oct 24 '21 at 02:46
  • I also tried the following: `static/reports/` `./static/reports` `static/reports` `reports/` `./reports/` –  Oct 24 '21 at 02:49
  • Can you update your question to reflect that, and post the error (as text)? – shreyasm-dev Oct 24 '21 at 02:50
  • I updated the post to reflect `./static/reports` and gave the text error. Thanks –  Oct 24 '21 at 02:52
  • 1
    Try using `__dirname + "/static/reports"`. I'm not sure if that's the problem, but it's worth a try. If that doesn't work, can you tell me the output of `console.log(__dirname)`? – shreyasm-dev Oct 24 '21 at 02:53
  • That was the ticket! I am still not sure how to access `/static/reports` from the web browser BUT the docker build didn't crash! Very smart to use the `__dirname +` for the pathing. I am going to research the rest of the information from here. I gave you the upvote for the answer. Thanks again so much!!! –  Oct 24 '21 at 03:01