var express = require('express');
var app = express();
var routes = require('./routes');
app.set('view engine', 'ejs');
app.locals.pagetitle = 'Awesome Website';
app.get('/', routes.index);
app.get('/about', routes.about);
// si on a autre chose
app.get('*', function (req, res) {
console.log('Page inconnue demandée');
res.render('default', { title: '😭', content: '😭😭😭' });
});
var server = app.listen(3000, function () {
console.log('Listening on port 3000.');
});
=== /routes/index.js ===
exports.index = function(req, res) {
res.render('default', {
title: 'Home',
content: 'Bonjour.',
users: ['Marie-Joëlle', 'Philippe', 'Jacques'],
})
}
exports.about = function(req, res) {
res.render('default', {
title: 'About us',
content: 'Ah ben c\'est nous !',
users: ['Marie-Joëlle'],
})
}
=== default.ejs ===
<% include partials/head.ejs %>
<%= pagetitle %> - <%= title %>
<%= content %>
<% if (typeof(users) !== 'undefined') { %>
Utilisateurs connectés
<% for (var i=0; i
- <%= users[i] %>
<% } %>
<% } %>