Express js static relative parent directory

后端 未结 2 473
遥遥无期
遥遥无期 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: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')));
    

提交回复
热议问题