Skip to content

Instantly share code, notes, and snippets.

@naholyr
Created May 27, 2011 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naholyr/995378 to your computer and use it in GitHub Desktop.
Save naholyr/995378 to your computer and use it in GitHub Desktop.
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
Copy link
Author

naholyr commented May 27, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment