Express.js and Moustache
First intall Express.js generator:
npm install express-generator -g
Display the command options with the -h option:
express -h
Create application:
express myapp
Then install dependencies:
cd myapp npm install
Run the app (on MacOS or Linux):
$ DEBUG=myapp npm start
In app.js default view engine by default is Jade:
app.set('view engine', 'jade');
First we need install this extension node-mustache-express:
npm install mustache-express --save
And then add in app.js:
var mustacheExpress = require('mustache-express'); ... app.engine('mustache', mustacheExpress()); app.set('view engine', 'mustache'); ...
Into directory 'views/templates' we create this file named 'index.mustache':
<p>Welcome to {{title}}!</p>
Go to http://localhost:3000 and see the resutl.
Let's do a new sample:
In file 'routes/index.js':
var express = require('express'); var router = express.Router(); var lusers=[ { name: 'Linus Torvalds', so: 'Linux' }, { name: 'Bill Gates', so: 'Windows XP' } ]; router.get('/', function(req, res, next) { res.render('index', { title: 'Express', lusers: lusers }); }); module.exports = router;
And in file 'views/index.mustache':
<p>Welcome to {{ title }}!</p> <ul> {{#lusers}} <li>{{name}} - {{so}}</li> {{/lusers}} </ul>
Now is time to play with {{ mustache }}!