表达js静态相对父目录 [英] Express js static relative parent directory

查看:70
本文介绍了表达js静态相对父目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前在通过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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆