Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

javascript - Express js static relative parent directory

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

My directory structure is as following:

  • public
    • css
    • lib
  • src
    • views
    • home
      • index.html
    • server.js

In my index.html file i have prefixed all my assets with a leading slash.

My static setup is as following: app.use(express.static(path.resolve(__dirname + '../' + 'public')));

But for some reason my static files are not getting served.

I was thinking that this is a crossdomain call or something... I'm currently using cloud9 IDE, might this have to do with it somehow?

question from:https://stackoverflow.com/questions/20322480/express-js-static-relative-parent-directory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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')));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...