Added posts to the reoute separation example

This commit is contained in:
Tj Holowaychuk 2010-11-13 10:55:15 -08:00
parent 14a22d9033
commit 5091c9e10f
8 changed files with 42 additions and 3 deletions

View File

@ -9,6 +9,7 @@ require.paths.unshift(__dirname + '/../../support');
var express = require('express')
, app = express.createServer()
, site = require('./site')
, post = require('./post')
, user = require('./user');
// Config
@ -32,5 +33,9 @@ app.get('/user/:id/view', user.view);
app.get('/user/:id/edit', user.edit);
app.put('/user/:id/edit', user.update);
// Posts
app.get('/posts', post.list);
app.listen(3000);
console.log('Express app started on port 3000');

View File

@ -0,0 +1,17 @@
// Fake posts database
var posts = [
{ title: 'Foo', body: 'some foo bar' }
, { title: 'Foo bar', body: 'more foo bar' }
, { title: 'Foo bar baz', body: 'more foo bar baz' }
];
exports.list = function(req, res){
res.render('post/list', {
locals: {
title: 'Posts'
, posts: posts
}
});
};

View File

@ -15,4 +15,10 @@ a.edit::before {
}
a.edit::after {
content: ' ]';
}
dt {
font-weight: bold;
}
dd {
margin: 15px;
}

View File

@ -1 +1,4 @@
<p>Visit the <a href="/users">users</a> page</p>
<ul>
<li>Visit the <a href="/users">users</a> page</li>
<li>Visit the <a href="/posts">posts</a> page</li>
</ul>

View File

@ -0,0 +1,7 @@
<h1>Posts</h1>
<dl id="posts">
<% posts.forEach(function(post){ %>
<dt><%= post.title %></dt>
<dd><%= post.body %></dd>
<% }) %>
</dl>

View File

@ -1,5 +1,5 @@
<h1>Editing <%= user.name %></h1>
<div id="user">
<h1>Editing <%= user.name %></h1>
<form method="post">
<input type="hidden" value="put" name="_method" />
<p>Name: <input type="text" value="<%= user.name %>" name="user[name]"/></p>

View File

@ -1,3 +1,4 @@
<h1>Users</h1>
<ul id="users">
<% users.forEach(function(user, id){ %>
<li>

View File

@ -1,4 +1,4 @@
<h1><%= user.name %></h1>
<div id="user">
<h1><%= user.name %></h1>
<p>Email: <%= user.email %></p>
</div>