我正在使用 Bower 制作一个 Express 应用程序,但不知何故找不到 Bower_components 文件夹。我在这里搜索过,尝试了所有解决方案,但没有任何效果。
在我的 server.js 中,我有:
/server/app.js
// Set static files
app.use(express.static('app'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
在索引中
/app/index.html
<link rel="stylesheet" href="/bower_components/bootstrap/dist/css/bootstrap.css" /></script>
<script src="bower_components/jquery/dist/jquery.js"></script>
<script src="bower_components/angular/angular.js"></script>
...
我得到了什么:
GET http://localhost:3000/bower_components/bootstrap/dist/css/bootstrap.css
(index):85 GET http://localhost:3000/bower_components/jquery/dist/jquery.js
(index):86 GET http://localhost:3000/bower_components/angular/angular.js
Cannot GET /bower_components/bootstrap/dist/css/bootstrap.css
...
如果我从标签中删除“/bower_components”前缀,并将服务器更改为:
server.js
app.use(express.static('bower_components'));
index.html
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.css" />
最佳答案
这应该是由服务器文件夹之外的 Bower_components 文件夹引起的。将 server/app.js
中的代码修改为:
var path = require('path');
app.use('/bower_components', express.static(path.dirname(__dirname) + '/bower_components'));
在这里,我使用 Nodejs 包中内置的 path
中的 dirname()
函数来获取服务器文件夹的父文件夹。
关于javascript - Bower_components 未使用 Express static 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38425079/