用于Bower组件的Express.js路由
我已经改变了我的Express.js项目使用鲍尔安装组件。 所有组件都安装在/ components(/components/jquery/jquery.js …等)下。
我也创build了自己的路由器,如下所示:
app.get('/', routes.index); // main page app.get('/p/:name', routes.p); //redirect routes app.get('/api/contacts', api.contacts); //look at all app.get('/api/contact/:id', api.contact); //look at one app.post('/api/contact', api.add); //add contact app.put('/api/contact/:id', api.edit); //edit&update contact app.delete('/api/contact/:id', api.delete); //delete contact
没有路由/组件,因此http://my.project/components/jquery/jquery.js
回来了一个Cannot GET /components/jquery/jquyery.js
有人可以让我知道什么是最好的方式添加路由/组件下的所有组件?
您可能想要使用静态中间件来执行此操作。 我不熟悉bower,但是如果所有组件都安装在/components
那么您可以执行以下操作:
app.use(express.static(__dirname + '/components'));
这意味着如果你有/components/jquery/jquery.js
你可以包含它
<script src='/jquery/jquery.js'></script>
如果你想用/components
前缀,你可以这样做:
app.use('/components', express.static(__dirname + '/components'));
这样你就可以请求脚本:
<script src='/components/jquery/jquery.js'></script>
如果你使用连接资产,像这样的东西效果很好:
app.use require("connect-assets")(paths: ['assets/js', 'assets/css', 'bower_components'])
然后在您的js清单中,您可以简单地包含像其他js资产一样的凉亭组件。 资产/ application.js中:
// bower components: //= require jquery/dist/jquery //= require underscore/underscore //= require backbone/backbone // local assets: //= require my_app