Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
250 additions
and
17 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
// Expose modules in ./support for demo purposes | ||
require.paths.unshift(__dirname + '/../../support'); | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var express = require('express') | ||
, app = express.createServer() | ||
, site = require('./site') | ||
, post = require('./post') | ||
, user = require('./user'); | ||
|
||
// Config | ||
|
||
app.set('view engine', 'ejs'); | ||
app.set('views', __dirname + '/views'); | ||
app.use(express.bodyDecoder()); | ||
app.use(express.methodOverride()); | ||
app.use(express.staticProvider(__dirname + '/public')); | ||
|
||
// General | ||
|
||
app.get('/', site.index); | ||
|
||
// User | ||
|
||
app.all('/users', user.list); | ||
app.all('/user/:id/:op?', user.load); | ||
app.get('/user/:id', user.view); | ||
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'); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
body { | ||
padding: 50px; | ||
font: 14px "Helvetica Neue", Arial, sans-serif; | ||
} | ||
a { | ||
color: #00AEFF; | ||
text-decoration: none; | ||
} | ||
a.edit { | ||
color: #000; | ||
opacity: .3; | ||
} | ||
a.edit::before { | ||
content: '[ '; | ||
} | ||
a.edit::after { | ||
content: ' ]'; | ||
} | ||
dt { | ||
font-weight: bold; | ||
} | ||
dd { | ||
margin: 15px; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
exports.index = function(req, res){ | ||
res.render('index', { | ||
locals: { | ||
title: 'Route Separation Example' | ||
} | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
// Fake user database | ||
|
||
var users = [ | ||
{ name: 'TJ', email: 'tj@vision-media.ca' } | ||
, { name: 'Tobi', email: 'tobi@vision-media.ca' } | ||
]; | ||
|
||
exports.list = function(req, res){ | ||
res.render('user/list', { | ||
locals: { | ||
title: 'Users' | ||
, users: users | ||
} | ||
}); | ||
}; | ||
|
||
exports.load = function(req, res, next){ | ||
var id = req.params.id; | ||
req.user = users[id]; | ||
if (req.user) { | ||
next(); | ||
} else { | ||
next(new Error('cannot find user ' + id)); | ||
} | ||
}; | ||
|
||
exports.view = function(req, res){ | ||
res.render('user/view', { | ||
locals: { | ||
title: 'Viewing user ' + req.user.name | ||
, user: req.user | ||
} | ||
}); | ||
}; | ||
|
||
exports.edit = function(req, res){ | ||
res.render('user/edit', { | ||
locals: { | ||
title: 'Editing user ' + req.user.name | ||
, user: req.user | ||
} | ||
}); | ||
}; | ||
|
||
exports.update = function(req, res){ | ||
// Normally you would handle all kinds of | ||
// validation and save back to the db | ||
var user = req.body.user; | ||
req.user.name = user.name; | ||
req.user.email = user.email; | ||
res.redirect('back'); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<ul> | ||
<li>Visit the <a href="/users">users</a> page</li> | ||
<li>Visit the <a href="/posts">posts</a> page</li> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<head> | ||
<title><%= title %></title> | ||
<link href="/style.css" rel="stylesheet" /> | ||
</head> | ||
<body> | ||
<%- 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<h1>Posts</h1> | ||
<dl id="posts"> | ||
<% posts.forEach(function(post){ %> | ||
<dt><%= post.title %></dt> | ||
<dd><%= post.body %></dd> | ||
<% }) %> | ||
</dl> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Editing <%= user.name %></h1> | ||
<div id="user"> | ||
<form method="post"> | ||
<input type="hidden" value="put" name="_method" /> | ||
<p>Name: <input type="text" value="<%= user.name %>" name="user[name]"/></p> | ||
<p>Email: <input type="text" value="<%= user.email %>" name="user[email]"/></p> | ||
<p><input type="submit" value="Save" /></p> | ||
</form> | ||
</div> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1>Users</h1> | ||
<ul id="users"> | ||
<% users.forEach(function(user, id){ %> | ||
<li> | ||
<a href="/user/<%= id %>"><%= user.name %></a> | ||
<a class="edit" href="/user/<%= id %>/edit">edit</a> | ||
</li> | ||
<% }) %> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<h1><%= user.name %></h1> | ||
<div id="user"> | ||
<p>Email: <%= user.email %></p> | ||
</div> |
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
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