javascript - Express.js routing with default route -


i have following express.js code:

router = express.router() fs.readdirsync('./controllers').foreach(function (file) {   if(file.substr(-3) == '.js') {       route = require('./controllers/' + file);       route.controller(router);   } }); app.use('/', router); 

it works good. don't set path '/' route in controllers, , express.js server renders 'index.html' 'public' folder default - want. want add '*' route, express.js return 'index.html' 'public' folder - it's static file, no need render, returning. how can it? thanks

if want service serve public/index.html root path can use express.static this:

app.use(express.static('public')); 

express.static treat index.html index file default, , can configure in 2nd argument:

app.use(express.static('public', {index: 'myindex.html'})) 

note don't have specify root app.use way are. this:

app.use(router); 

i suggest route.controller() methods not necessary. if each of controllers exported own express.router() app.use(mycontroller) in iterator.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -