Html 为什么node.js应用程序CSS加载在我的IDE中,而不是本地主机上?
我正在使用LightTable制作一个nodejs应用程序,当我在Html 为什么node.js应用程序CSS加载在我的IDE中,而不是本地主机上?,html,css,path,localhost,static-resource,Html,Css,Path,Localhost,Static Resource,我正在使用LightTable制作一个nodejs应用程序,当我在标记中指示文件的相对路径时,css似乎加载良好。然而,当我运行node index.js时,服务器启动,页面加载正常,但我得到一个控制台错误,我自己的404表示在指定的路径上找不到CSS文件。我也试过使用Heroku的《领班入门》,但没有骰子。以下是html: <!DOCTYPE html> <html> <head> <title>
标记中指示文件的相对路径时,css似乎加载良好。然而,当我运行node index.js
时,服务器启动,页面加载正常,但我得到一个控制台错误,我自己的404表示在指定的路径上找不到CSS文件。我也试过使用Heroku的《领班入门》,但没有骰子。以下是html:
<!DOCTYPE html>
<html>
<head>
<title>
Home
</title>
<link href="../../styles/master.css" rel="stylesheet" type="text/css" />
</head>
...
编辑:找到问题的答案后,以下信息变得相关。我已经注释掉了使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);
}
});
//路由
var homeREGEX=newregexp(“^/?$”);
var newREGEX=newregexp(“^/posts/new/?$”);
var postsREGEX=new RegExp(“^/posts/?$”);
//var cssREGEX=new RegExp(“^/styles/master.css/?$”) 查看页面时,您在浏览器中使用的url是什么?我的猜测是,is不包含视图/get
部分,这意味着您不需要链接href属性中的。\..
另外,确保您的服务器实际上正在为这些静态文件提供服务(看起来您并没有)。例如,如果您使用的是express(与nodejs一起使用),则需要以下内容:
app.use(express.static('resources'));
app.use(express.static('style'));
请参阅。也许您可以尝试放置精确路径,而不是..//
<link href="/Procfile/styles/master.css" rel="stylesheet" type="text/css" />
当您查看页面时,您在浏览器中看到的URL是什么?localhost:3000,但如果我导航到localhost:3000/styles/master.css,则会出现404 not found错误。我已尝试删除并添加了我能想到的所有组合中的“./”。还没开始工作!好的,但是请给出你的url,否则我们无能为力。我把它贴在上面:localhost:3000。在控制台中,它显示x-GET localhost:3000/styles/master.css
,并在我导航到那里时确认了一个404未找到错误。您确定您确实在为该文件提供服务吗?看看我最新的答案。实际上,这可能是我的问题。我不使用Express,因为我想了解内部结构,然后构建框架。在没有Express的情况下,我如何执行类似app.use的操作?
<link href="/Procfile/styles/master.css" rel="stylesheet" type="text/css" />