繁体   English   中英

bower_components无法以静态表达方式加载

[英]bower_components not loading with express static

我正在使用Bower制作快速应用程序,但是不知为何找不到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包中内置pathdirname()函数来获取服务器文件夹的父文件夹。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM