html - 为什么 node.js 应用程序 CSS 在我的 IDE 中加载而不是在本地主机上加载?

标签 html css path localhost static-resource

我正在使用 LightTable 创建一个 nodejs 应用程序,当我在 <link href=""> 中指示文件的相对路径时,css 似乎加载正常标签。当我运行 node index.js ,但是,服务器启动并且页面加载正常,但我收到控制台错误和我自己的 404 提示无法在指定路径找到 CSS 文件。我尝试使用 Heroku 的 Foreman Start同样,但没有骰子。这是 HTML:

<!DOCTYPE html>
<html>
    <head>
        <title>
            Home
        </title>
        <link href="../../styles/master.css" rel="stylesheet" type="text/css" />
    </head>
...

这是我的目录树结构:

.
├── Procfile
├── README.md
├── index.js
├── package.json
├── resources
│   └── images
│       └── tokyo.jpg
├── styles
│   └── master.css
└── views
    ├── get
    │   ├── home.html
    │   └── posts.html
    └── post
        └── new.html 

编辑:找到问题的答案后,以下信息变得相关。我已经注释掉了根据需要加载 CSS 和背景图像的新代码。

这是我的路由和服务器代码。我不是故意使用 Express 的:

    // routes
    var homeREGEX = new RegExp('^/?$');
    var newREGEX = new RegExp('^/posts/new/?$');
    var postsREGEX = new RegExp('^/posts/?$');
 // var cssREGEX = new RegExp('^/styles/master.css/?$');             <--| static resources also
 // var tokyoREGEX = new RegExp('^/resources/images/tokyo.jpg/?$');  <--| need routes. 

    // server
    var server = http.createServer(function(request, response){
      var pathname = url.parse(request.url).pathname;

      if (homeREGEX.test(pathname)) {
        renderHome(request, response);

      } else if (newREGEX.test(pathname)) {
        renderPostForm(request, response);
      } else if (postsREGEX.test(pathname)) {
        addNewPost(request, response);
   // } else if (cssREGEX.test(pathname)) {           <--| and they need to get
   //   sendCSS(request, response);                   <--| served individually as
   // } else if (tokyoREGEX.test(pathname)) {         <--| they are requested
   //   sendTokyo(request, response);
      } else {
        error404(request, response);
      }
    });

最佳答案

您在查看页面时在浏览器中使用的 url 是什么?我的猜测是不包含 views/get部分,这意味着您不需要 ..\..在您的链接 href 属性中。

此外,请确保您确实在提供这些静态文件(看起来您不是)。例如,如果您使用 express(与 nodejs 一起),您将需要这样的东西:

app.use(express.static('resources'));
app.use(express.static('style'));

参见 express.static documentation .

关于html - 为什么 node.js 应用程序 CSS 在我的 IDE 中加载而不是在本地主机上加载?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26010111/

相关文章:

html - 不允许浏览器缩放文本

html - 使用高度 100% 会将元素抛出屏幕

mysql - LOAD DATA LOCAL INFILE 找不到路径

javascript - 在可滚动的固定定位元素上滚动时如何在标题上添加/删除类?

javascript - 循环确定 selectedIndex 不起作用?

jquery - vimeo html5 编辑 CSS

jquery - 图像顶部的可缩放和可拖动标签

html - 如何对齐表单内的 SVG 按钮

python - python 中的相对模块路径 - 如何配置路径(和 PyCharm)

path - Java 窗口命令