User Tools

Site Tools


bwnjsejs:modularizing_our_routes

This is an old revision of the document!


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

Page inconnue demandée Page inconnue demandée fixdns^C leo@noname ~/Bureau/node/1 $ fixdns [sudo] password for leo: Sorry, try again. [sudo] password for leo: leo@noname ~/Bureau/node/1 $ fixdns leo@noname ~/Bureau/node/1 $ ping -c 4 letemps.ch PING letemps.ch (198.41.204.105) 56(84) bytes of data. 64 bytes from 198.41.204.105: icmp_seq=1 ttl=54 time=48.2 ms 64 bytes from 198.41.204.105: icmp_seq=2 ttl=54 time=47.0 ms 64 bytes from 198.41.204.105: icmp_seq=3 ttl=54 time=45.9 ms

— letemps.ch ping statistics — 3 packets transmitted, 3 received, 0% packet loss, time 2001ms rtt min/avg/max/mdev = 45.974/47.117/48.291/0.978 ms leo@noname ~/Bureau/node/1 $ ping -c 4 wiki.leomartin.net PING wiki.leomartin.net (213.186.33.4) 56(84) bytes of data.

— wiki.leomartin.net ping statistics — 4 packets transmitted, 0 received, 100% packet loss, time 3024ms

leo@noname ~/Bureau/node/1 $ ping -c 4 wiki.leomartin.net PING wiki.leomartin.net (213.186.33.4) 56(84) bytes of data.

— wiki.leomartin.net ping statistics — 4 packets transmitted, 0 received, 100% packet loss, time 3024ms

leo@noname ~/Bureau/node/1 $ ping -c 4 wiki.leomartin.net PING wiki.leomartin.net (213.186.33.4) 56(84) bytes of data.

<!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.1462731531.txt.gz · Last modified: 2016/05/08 20:18 by leo