Created
May 27, 2011 14:39
-
-
Save naholyr/995378 to your computer and use it in GitHub Desktop.
Application bidon "forum" d'exemple
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** Liste des threads du forum */ | |
const threads = [ | |
{ id: 1, title: 'First thread' }, | |
{ id: 33, title: 'Oh noes, Jesus died' }, | |
{ id: 42, title: 'Ze answer' } | |
] | |
/** Déclaration de l'application */ | |
var app = require('express').createServer().configure(function() { | |
/** Les vues sont dans "./views" */ | |
this.set('views', __dirname + '/views') | |
/** On utilise des templates EmbedJS */ | |
this.set('view engine', 'ejs') | |
/** Ces variables seront toujours disponibles dans les vues, | |
ça évite de redéclarer des variables communes du layout par exemple */ | |
this.set('view options', { | |
title: 'Forum' | |
}) | |
/** Liste des threads sur "/" ou "/threads" */ | |
function listThreads(req, res, next) { | |
res.render('index', { threads: threads }) | |
} | |
this.get('/', listThreads) | |
this.get('/threads', listThreads) | |
/** Détail d'un thread sur "/thread/:id" */ | |
function detailThread(req, res, next) { | |
var thread = threads.filter(function(t) { return t.id==req.params.id })[0] | |
if (!thread) { | |
res.send(404) | |
} else { | |
res.render('thread', { thread: thread }) | |
} | |
} | |
this.get('/thread/:id', detailThread) | |
}) | |
/** On démarre le serveur sur le port 8080 */ | |
app.listen(8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"author": "", | |
"name": "forum", | |
"description": "Application bidon, pour exemple de réutilisation", | |
"version": "0.0.1", | |
"repository": { | |
"url": "" | |
}, | |
"engines": { | |
"node": "*" | |
}, | |
"dependencies": { | |
"express": "*", | |
"ejs": "*" | |
}, | |
"devDependencies": {}, | |
"main": "app.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2>Threads</h2> | |
<ul> | |
<% for (var i=0, nbThreads=threads.length; i<nbThreads; ++i) { %> | |
<li><a href="/thread/<%= threads[i].id %>">Thread #<%= threads[i].id %> | |
<% } %> | |
</ul> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head><title><%= title %></title></head> | |
<body> | |
<h1>Forum - <%= title %></h1> | |
<%- body %> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h2><%= thread.title %></h2> | |
<p>Content of the thread...</p> | |
<p><a href="/threads">Back to index</a></p> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Démo disponible sur http://demoexpress1.naholyr.dotcloud.com/