我正在使用 ExpressJS。我的脚本或 CSS 无法加载。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta name="apple-mobile-web-app-capable" content="yes"/>
<meta name="apple-mobile-web-app-status-bar-style" content="black"/>
<title>Customer Info</title>
<link rel="stylesheet" href="/stylesheets/testing.css" type="text/css" media="screen" charset="utf-8"/>
<script type="text/javascript" src="/javascripts/jquery-1.9.1.js"></script>
<script src="/javascripts/jquery-ui-1.10.2.custom.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="/javascripts/testing.js"></script>
</head>
<body>
//body contents
</body>
</html>
我知道问题不在于位置。当我以 express 方式呈现此 EJS View 时,没有任何脚本正在加载。我不明白为什么它适用于几乎相同的 View ,但不适用于这个 View 。有任何想法吗?
最佳答案
第一步
在您的 node.js
文件中找到您配置 express 的部分。
就像。
var express = require('express')
, app = express();
app.configure(function() {
//configure the express codes go here
}
在其中检查您配置提供静态文件的文件夹的代码行。像图像、css 和 js。我会喜欢。
app.use(express.static(__dirname + '/public'));
在上面的例子中,public
应该是包含您在上面的 html。
喜欢
--app.js
--package.json
--views/
--node_modules/
--public/
--stylesheets/
--testing.css
--javascripts/
--jquery-1.9.1.js
--jquery-ui-1.10.2.custom.js
--testing.js
第二步
如果上面配置正确那么你可以确定
app.use(express.static(__dirname + '/public'));
在配置中先于
app.use(app.router);
这样您就可以确保您的线路不会被您在路由器中编写的 404 路由处理程序忽略。
第 3 步
您可以使用 Firebug
或 Chrome inspector
检查呈现的页面,并在网络选项卡中检查您收到的对您提到的 js 和 css 文件的响应是什么。
根据您的问题,它应该是 404 file not found,如果是其他问题,您应该在问题中更新它。
第四步
如果您仍然无法解决问题,那么您必须使用您正在使用的 node.js 代码更新问题。以便我们进行调查。
关于html - 脚本未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15841539/