0

I have an express application which is provided from a module which in turn is located in node_modules like this:

home
  user
    project
      app.js
      dist
      main.css
      an.svg
      node_modules
        static-webserver
          server.js
          public
            dev
              websocket.js

app.js invokes the express static-webserver like this:

require('static-webserver/server.js');

In static-webserver/server.js I am setting two particular paths. One path, which maps /dev to static-websserver's public/dev directory maps like this:

app.use('/dev/', express.static(__dirname + '/public/dev/'));

This path is is working.

But the second path

app.get('/dist', express.static(Path.resolve('dist')));

is not resolving to the dist directory of the application. Path.resolve('dist') should resolve an absolute path /user/project/dist/ but it does not. It always tries to resolve to /home/user/project/node_modules/static-webserver/dist, which is not what I need.

Is there any way to make express static accept absolute paths?

LongHike
  • 4,016
  • 4
  • 37
  • 76

1 Answers1

0

If the absolute path to the /dist directory is known, I suggest not using Path.resolve and instead using process.cwd(), or __dirname (in combination) to access /dist.


app.get('/dist', express.static(process.cwd() + '/dist'));

ref: https://stackoverflow.com/a/9874415/16471349

Manny
  • 369
  • 2
  • 7
  • It's not working either. process.cwd() simply returns an absolute path as String. It's the same as when I use path.resolve(). And despite the path pointing to /home/user/project node does seem to confine file access to the static-webserver (the dependency module) location. And the more I think about it, the more it makes sense. Because that would constitute a security problem. That's my take on this. But thanks anyway for your effort. – LongHike Jul 23 '21 at 05:45