Use of mustache-express4

1

I'm a novice with express and mustache , but I do not understand the error:

index.js

const join = require( 'path' ).join;
const EX = require( 'express' );
const MiniModule = require( './mini' );

var app = EX( );

app.engine( 'must', require( 'mustache-express4' ) );
app.set( 'views', join( __dirname, 'views' ) );
app.set( 'view engine', 'must' );

app.use( MiniModule );

app.listen( 8080 );

and mini.js

const EX = require( 'express' );

var Router = EX.Router( );

Router.get( '/login.html', function( req, res ) {
  res.render( 'login.must', { title: 'Prueba de mustache' } );
} );

module.exports = Router;

The template I try to render is very simple:

File login.must :

<html>
<head><title>{{title}}</title></head>
<body></body>
</html>

The directory hierarchy of the project is as follows:

Proyecto/
+-node_modules/
| +- ...
+-src/
  +-index.js
  +-mini.js
  +-views/
    +-login.must

When executing index.js , it does not show any error. The server boots and listens on port 8080 .

But ... as soon as I try to access, with the browser, to

localhost:8080/login.html

I get the following error:

  

fs.js: 952
  return binding.readdir (pathModule._makeLong (path), options.encoding);
  ------------------- ^

  TypeError: path must be a string or Buffer
     at TypeError (native)
     at Object.fs.readdirSync (fs.js: 952: 18)
     at loadPartials (Project / node_modules / mustache-express4 / lib / mustache-express.js: 11: 14)
     at Project / node_modules / mustache-express4 / lib / mustache-express.js: 28: 63
     at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js: 446: 3)

  • What am I doing wrong?
  • How do I solve it?
asked by 15.04.2017 в 12:11
source

1 answer

1
  

What am I doing wrong?

  • You use must when it should be mustache .
  • You forgot to set the directory of partial views.
  

How do I solve it?

First, change must by mustache :

app.engine( 'mustache', require( 'mustache-express4' ) );
app.set( 'views', join( __dirname, 'views' ) );
app.set( 'view engine', 'mustache' );

Also, Mustache asks you to set up the partial views directory so you can search there if one is used. Therefore, as a second thing you must add the directory of partial views:

app.set('partials', join(__dirname, 'views/partials'));
    
answered by 15.04.2017 / 19:35
source