2

I need to register multiple views folders using Express and express-handlebars. Is it posible?

I have this structure:

/project/
    /admin/
        /views/
        /server/
    /webapp/
        /views/
        /server/
    /common/
    /node_modules/

And I need that when I render a view in a route I be able to access to one of these folders views /admin/views/ or /webapp/views/.

  • For this functionality, you need to do authentication first, then you need to check if admin logged in the application then redirect admin to another views & client logged in the application then redirect admin to another view...I don't know why you have created separation for that. – Parth Raval Oct 23 '19 at 09:45
  • I want to have a separated folders to organize better my app, I know that I can do this, but that's not the question :) – Noelia Romero Oct 23 '19 at 10:17

1 Answers1

1

Starting from express version 4 you can do this by passing an array of directories app.set(name, value) like this:

app.set('views', [
  path.join(__dirname, 'app/index'),
  path.join(__dirname, 'app/error'),
]);

Naming conflict:

Be careful when you doing this so you don't get naming conflict. For example if you have two files both named error.ejs one inside app/index and the other inside app/error, express will serve the one inside in app/index since the documentation state:

the views are looked up in the order they occur in the array.

Yamona
  • 1,070
  • 1
  • 16
  • 36