配置节点express以服务于静态bower_components? [英] configure node express to serve static bower_components?
本文介绍了配置节点express以服务于静态bower_components?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个目录结构
projectName
| - bower_components/
| - public/
| - css
| - js
| - index.html
| - Gruntfile.js
| - package.json
| - bower.json
| - app.js
我想启动我的应用程序并提供索引。 html
与节点。所以在 app.js
我有:
I would like to start my app and serve index.html
with node. So in app.js
I have:
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):
When I start the server, index.html
shows up but none of the above libraries load. I get the error (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提供文件?
How can I serve the files from bower_components?
推荐答案
我使用这个设置:
app.use(express.static(__dirname + '/public'));
app.use('/bower_components', express.static(__dirname + '/bower_components'));
所以任何Bower组件都是从HTML加载的:
So any Bower components are loaded from HTML like this:
<script src="/bower_components/..."></script>
任何其他客户端JS / CSS(在 public / code>)被加载如下:
And any other client-side JS/CSS (in public/
) are loaded like this:
<script src="/js/..."></script>
这篇关于配置节点express以服务于静态bower_components?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文