bwnjsejs:modularizing_our_routes
index.js
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
<!DOCTYPE html> <html> <head><% include partials/head.ejs %></head> <body> <h1><%= pagetitle %> - <%= title %></h1> <p><%= content %></p> <% if (typeof(users) !== 'undefined') { %> <h2>Utilisateurs connectés</h2> <ul> <% for (var i=0; i <users.length; i++) {%> <li><%= users[i] %></li> <% } %> </ul> <% } %> </body> </html>
bwnjsejs/modularizing_our_routes.txt · Last modified: 2016/05/08 20:28 by leo