表达js静态相对父目录 [英] Express js static relative parent directory
问题描述
我目前在通过expressJs提供静态文件时遇到一些小问题。
I'm currently experiencing some minor problems with serving static files through expressJs.
我的目录结构如下:
- public
- css
- lib
- views
- home
- index.html
在我的
index.html
文件中我使用前导斜杠为我的所有资产加上前缀。In my
index.html
file i have prefixed all my assets with a leading slash.我的静态设置如下:
app.use(express.static (path.resolve(__ dirname +'../'+'public')));
但由于某些原因,我的静态文件是没有得到服务。
But for some reason my static files are not getting served.
我以为这是一个跨域调用或者什么......
我目前正在使用cloud9 IDE,这可能是必须要做的以某种方式吗?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?
推荐答案
你应该使用path.join 而不是手动连接路径组件。它使用 path.normalize ,它解析
。
和..
,处理多个或尾部斜杠,并为您的平台使用适当的文件分隔符(请参阅: path.sep )。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).例如,
var path = require('path'); var express = require('express'); var app = express(); app.use(express.static(path.join(__dirname, '../public')));
这篇关于表达js静态相对父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!