1

I'm writing a middleware function at the moment and need a way to check the view engines that have been registered to the current app.

As it is possible in express to have two view engines:

app.engine('pug',...);
app.engine('jade',...);

app.set('view engine','pug');
app.set('view engine','jade');
//To send view
res.render('index.pug',{viewData:{}});
res.render('index.jade',{viewData:{}});

I need a way to determine the extensions of all current view engines. However, upon checking app.get('view engine') or app.settings['view engine'] it is simply set to a string (with the value of the last call the set), rather than an array as I would have expected.

How can I get the information I need?

Eladian
  • 958
  • 10
  • 29

1 Answers1

1

view engine is defined as "The default engine extension to use when omitted". Therefore, you can only have one view engine at a time. Calls of app.set('view engine', ...) overwrite the default.

To get all available engines (set up with calls to app.engine), you can use the engines property. For instance,

console.log(Object.keys(app.engines));
// output: [ '.pug', '.jade' ]

However, that property is not part of the API, so it may break in future express.js versions.

phihag
  • 278,196
  • 72
  • 453
  • 469