Express js static relative parent directory

后端 未结 2 469
遥遥无期
遥遥无期 2020-12-28 12:58

I\'m currently experiencing some minor problems with serving static files through expressJs.

My directory structure is as following:

  • public
    • c
相关标签:
2条回答
  • 2020-12-28 13:35

    __dirname doesn't have a trailing slash, so __dirname + '../' results in DIRNAME../ (which isn't valid).

    Instead, use:

    __dirname + '/../public'
                 ^ extra slash
    
    0 讨论(0)
  • 2020-12-28 13:39

    You should use path.join instead of manually concatening path components. It uses path.normalize, which resolves . and .., handles multiple or trailing slashes, and uses the appropriate file separator for your platform (see: path.sep).

    For example,

    var path = require('path');
    
    var express = require('express');
    
    var app = express();
    
    app.use(express.static(path.join(__dirname, '../public')));
    
    0 讨论(0)
提交回复
热议问题