I'm making an express app using bower, but somehow the bower_components folder isn't found. I've searched here, tried all solutions, but nothing worked.
我正在使用bower制作快速应用程序,但不知何故找不到bower_components文件夹。我在这里搜索过,尝试了所有解决方案,但没有任何效果。
In my server.js I have:
在我的server.js中,我有:
/server/app.js
// Set static files
app.use(express.static('app'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
In the index
在索引中
/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>
...
And what I get:
而我得到的:
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
...
It works if I remove the "/bower_components" prefix from the tags, and change in server to this:
如果我从标签中删除“/ bower_components”前缀,并将服务器更改为:
server.js
app.use(express.static('bower_components'));
index.html
<link rel="stylesheet" href="/bootstrap/dist/css/bootstrap.css" />
1 个解决方案
#1
1
This should be caused by bower_components folder outside your server folder. Modify your code at server/app.js
to this:
这应该是由服务器文件夹外的bower_components文件夹引起的。将server / app.js上的代码修改为:
var path = require('path');
app.use('/bower_components', express.static(path.dirname(__dirname) + '/bower_components'));
Here i'm using the dirname()
funtion from the path
built in nodejs package to get the parent folder of your server folder.
这里我使用nodejs包中内置路径的dirname()函数来获取服务器文件夹的父文件夹。
#1
1
This should be caused by bower_components folder outside your server folder. Modify your code at server/app.js
to this:
这应该是由服务器文件夹外的bower_components文件夹引起的。将server / app.js上的代码修改为:
var path = require('path');
app.use('/bower_components', express.static(path.dirname(__dirname) + '/bower_components'));
Here i'm using the dirname()
funtion from the path
built in nodejs package to get the parent folder of your server folder.
这里我使用nodejs包中内置路径的dirname()函数来获取服务器文件夹的父文件夹。