I had the same problem you encountered and you have 2 possible solutions!
//////////////////////////////
Solution # 1: List of directories that you make / donโt want to โserviceโ
//////////////////////////////
How to do:
Let's say your directory structure looks like this:
-app/ ---controllers/ ---directives/ ---etc. -public/ ---img/ ---css/ ---index.html ---etc. -views/ ---home.html ---vehicle.html ---etc. -app.js (run with 'node app.js')
Some of these 3 folders (applications, public, views) have different rules according to which you want to serve and which you want to display:
- Appendix /
- serve: If your site does GET / app / controller.js, you want to serve them in a literal file.
- public /
- serve: If your site does GET / public / img / logo.jpg, you want to serve them with a literal file.
- view /
- render: If your site does GET views / home, you want to display their view from your angular.js route.
Now, from your node / express js file, find or add the following:
//This tells node that these folders are for STATIC SERVING files (meaning literal files). //And Yes! You can use multiple to indicate multiple directories. app.use(express.static(path.join(__dirname, 'app'))); app.use(express.static(path.join(__dirname, 'public')));
Additional Information:
app.use(express.static(__dirname + '/client/views')); means that you are serving literal files from / client / views, but nothing from this directory. See angularjs index.html in the views folder
Extended example:
This will be your possible configuration:
//app.js var express = require('express'), bodyParser = require('body-parser'), methodOverride = require('method-override'), errorHandler = require('error-handler'), morgan = require('morgan'), routes = require('./routes/index'), //My special routes file http = require('http'), path = require('path'); var app = module.exports = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(morgan('dev')); app.use(bodyParser()); app.use(methodOverride()); app.use(express.static(path.join(__dirname, 'app'))); app.use(express.static(path.join(__dirname, 'public'))); // serve index and view partials app.get('/', routes.index); //Partials app.get('/partials/:name', routes.partials); // redirect all others to the index // BUT the static files listed above still get served statically app.get('*', routes.index); http.createServer(app).listen(app.get('port'), function () { console.log('Express server listening on port ' + app.get('port')); });
Then this will be your routes file:
exports.index = function(req, res){ res.render('index'); }; exports.partials = function (req, res) { var name = req.params.name; res.render('partials/' + name); };
//////////////////////////////
Solution # 2: check if the file exists first
//////////////////////////////
How to do:
Keep everything as you already have, but instead of trying "res.sendFile" right away, check if it exists first. For instance:
exports.all = function (req, res) { var name = req.params[0]; fs.exists(path+'/'+name, function(exists){ if(exists) {
Extended example:
This will be your possible configuration:
//app.js var express = require('express'), bodyParser = require('body-parser'), methodOverride = require('method-override'), errorHandler = require('error-handler'), morgan = require('morgan'), routes = require('./routes/index'), //My special routes file http = require('http'), path = require('path'); var app = module.exports = express(); // all environments app.set('port', process.env.PORT || 3000); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.use(morgan('dev')); app.use(bodyParser()); app.use(methodOverride()); app.use(express.static(path.join(__dirname, ''))); // serve index and view partials app.get('/', routes.index); //Partials app.get('/partials/:name', routes.partials); // redirect all others app.get('*', routes.all); http.createServer(app).listen(app.get('port'), function () { console.log('Express server listening on port ' + app.get('port')); });
and this is your routes file:
var fs = require('fs'); var path = require('path'); exports.index = function(req, res){ res.render('index'); }; exports.partials = function (req, res) { var name = req.params.name; res.render('partials/' + name); }; exports.all = function (req, res) { var name = req.params[0]; fs.exists(path+'/'+name, function(exists){ if(exists) {
//////////////////////////////
Hope this helps!