Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Application bidon "forum" d'exemple
/** 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)
{
"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"
}
<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>
<html>
<head><title><%= title %></title></head>
<body>
<h1>Forum - <%= title %></h1>
<%- body %>
</body>
</html>
<h2><%= thread.title %></h2>
<p>Content of the thread...</p>
<p><a href="/threads">Back to index</a></p>
@naholyr
Owner
naholyr commented May 27, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.