我有一个目录结构
projectName
| - bower_components/
| - public/
| - css
| - js
| - index.html
| - Gruntfile.js
| - package.json
| - bower.json
| - app.js
我想要使用node启动我的应用并提供index.html
文件。因此,在app.js
中,我有如下代码:
var express = require('express');
var port = process.env.PORT || 3000;
var app = express();
app.configure(function(){
// Serve up content from public directory
app.use(express.static(__dirname + '/public'));
app.use(app.router);
app.use(express.logger());
});
app.listen(port, function(){
console.log('Express server listening on port ' + port);
});
我在index.html
底部添加了以下内容:
<script src="../bower_components/jquery/jquery.js"></script>
<script src="../bower_components/d3/d3.js"></script>
<script src="../bower_components/bootstrap/dist/js/bootstrap.js"></script>
<script src="bower_components/spin.js/spin.js"></script>
<script src="bower_components/mustache/mustache.js"></script>
当我启动服务器时,index.html
文件会显示出来,但是上面的库都没有加载。我收到了一个错误(404):
启动服务器后,index.html
页面会显示,但上述的库并未加载,出现了404错误提示:
GET http://localhost:3000/bower_components/jquery/jquery.js 404 (Not Found) localhost/:32
GET http://localhost:3000/bower_components/d3/d3.js 404 (Not Found) localhost/:33
GET http://localhost:3000/bower_components/bootstrap/dist/js/bootstrap.js 404 (Not Found) localhost/:34
GET http://localhost:3000/bower_components/spin.js/spin.js 404 (Not Found) localhost/:35
GET http://localhost:3000/bower_components/mustache/mustache.js 404 (Not Found)
我该如何提供来自bower_components的文件?
package.json
和bower.json
是否会相互冲突?例如对于名称、版本甚至是同一包的不同版本的依赖项等字段,你应该将它们放在package.json
还是bower.json
中呢? - Nam Thaipackage.json
用于 npm 依赖 (通常是服务器端或构建系统的要求),而bower.json
则仅用于客户端依赖,例如 bootstrap、angular 或 jquery。它们彼此不冲突。 - Connor Leech