我正在使用LightTable製作nodejs應用程序,並且當我在<link href="">
標記中指示文件的相對路徑時,css似乎正在加載正常。但是,當我運行node index.js
時,服務器啓動並且頁面加載正常,但是出現控制檯錯誤,我自己的404說在指定的路徑中找不到CSS文件。我也試過使用Heroku的Foreman Start
,但沒有骰子。下面是HTML:爲什麼node.js應用CSS在我的IDE中加載,但不在本地主機上?
<!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);
}
});
什麼是你在瀏覽器中看到,當你在看網頁,這是行不通的網址是什麼? – 2014-09-24 06:39:19
localhost:3000,但是如果我導航到localhost:3000/styles/master.css,我得到一個404找不到,錯誤。 – kurofune 2014-09-24 06:51:57