为什么我无法从 Expressjs/Nodejs 访问我的 javascript 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8988880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why can't I get to my javascript files from Expressjs/Nodejs?
提问by Simon Lomax
I'm starting to experiment with nodejs/expressjs/coffeescript and the jade view engine.
我开始尝试使用 nodejs/expressjs/coffeescript 和 jade 视图引擎。
I have the following setup which from what I can see from the examples that are around seems pretty standard.
我有以下设置,从我从周围示例中看到的内容来看,这似乎非常标准。
app = express.createServer().listen process.env.PORT
app.configure ->
app.set 'views', __dirname + '/views'
app.set 'view engine', 'jade'
app.set 'view options', layout: true
app.use express.bodyParser()
app.use express.static(__dirname + '/public')
app.use app.router
app.get '/ekmHoliCal/index', (req, res) ->
res.render 'index'
My directory structure is as follows:
我的目录结构如下:
-- ekmHoliCal
-- public
--javascripts
client.js
-- views
index.jade
layout.jade
The index file contains nothing but the the line: This is the index file
索引文件只包含以下行: 这是索引文件
the layout.jade file contains:
layout.jade 文件包含:
!!! 5
html(lang="en")
head
title Users
script(type="text/javascript", src="http://code.jquery.com/jquery-1.6.4.js")
script(type="text/javascript", src="/ekmholical/javascripts/client.js")
body
div!= body
When I navigate to localhost/ekmHoliCal/index I get served the index page as expected. However if I view source I see a link to to jquery :
当我导航到 localhost/ekmHoliCal/index 时,我得到了预期的索引页面。但是,如果我查看源代码,我会看到一个指向 jquery 的链接:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.6.4.js">
and if I click on that link sure enough I see the source of the jquery file.
如果我确实单击该链接,我会看到 jquery 文件的来源。
The index file source also contains a link to my client.js file as follows:
索引文件源还包含指向我的 client.js 文件的链接,如下所示:
<script type="text/javascript" src="/ekmholical/javascripts/simon.js"></script>
but when I click on that I get
但是当我点击它时,我得到
Cannot GET /ekmholical/javascripts/client.js
无法获取 /ekmholical/javascripts/client.js
I've seen this related question (Express-js can't GET my static files, why?) and can't see what I'm doing wrong
我已经看到了这个相关问题(Express-js 无法获取我的静态文件,为什么?)并且看不到我做错了什么
回答by Paul Armstrong
Your script tag has the wrong path. it should be
您的脚本标签路径错误。它应该是
<script type="text/javascript" src="/javascripts/simon.js"></script>
In express, you've set the following:
在 express 中,您设置了以下内容:
app.use express.static(__dirname + '/public')
That tells express/node that the publicdirectory should act as your web root. Everything in it can be referenced via /, so if you also have a CSS folder in there, you might use /css/styles.css
这告诉 express/node 该public目录应该作为你的 web 根目录。里面的所有东西都可以通过 引用/,所以如果你也有一个 CSS 文件夹,你可以使用/css/styles.css
回答by codejockie
Following up on what Paul Armstrong said, you could also do this.
按照保罗·阿姆斯特朗的说法,你也可以这样做。
app.use('/public', express.static(__dirname + '/public');
This will allow you to reference your js file like this:
这将允许您像这样引用您的 js 文件:
<script src="/public/javascripts/simon.js"></script>

