Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

configure node express to serve static bower_components?

I have a directory structure

projectName     | - bower_components/     | - public/         | - css         | - js         | - index.html     | - Gruntfile.js     | - package.json     | - bower.json     | - 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); }); 

At the bottom of index.html I have:

<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> 

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)  

How can I serve the files from bower_components?

like image 526
Connor Leech Avatar asked Feb 17 '14 05:02

Connor Leech


People also ask

How do I serve a static file in node?

In your node application, you can use node-static module to serve static resources. The node-static module is an HTTP static-file server module with built-in caching. First of all, install node-static module using NPM as below. After installing node-static module, you can create static file server in Node.

Which Express JS function is used to serve static files?

To serve static files such as images, CSS files, and JavaScript files, use the express. static built-in middleware function in Express. The root argument specifies the root directory from which to serve static assets.


2 Answers

I use this setup:

app.use(express.static(__dirname + '/public')); app.use('/bower_components',  express.static(__dirname + '/bower_components')); 

So any Bower components are loaded from HTML like this:

<script src="/bower_components/..."></script> 

And any other client-side JS/CSS (in public/) are loaded like this:

<script src="/js/..."></script> 
like image 134
robertklep Avatar answered Oct 13 '22 23:10

robertklep


You should use

app.use(express.static(path.join(__dirname, '/public'))); app.use('/bower_components',  express.static( path.join(__dirname, '/bower_components'))); 

Note the usage of (path.join) which is different from @robertklep answer

Here is a note from Another SO questions which according to me captures it very well

path.join will take care of unneccessary delimiters, that may occur if the given pathes come from unknown sources (eg. user input, 3rd party APIs etc.).

So path.join('a/','b') path.join('a/','/b'), path.join('a','b') and path.join('a','/b') will all give a/b.

Without using it, you usually would make expectations about the start and end of the pathes joined, knowing they only have no or one slash.

like image 22
Abhijit Mazumder Avatar answered Oct 14 '22 01:10

Abhijit Mazumder