Permalink
Browse files

Merge branch 'examples'

  • Loading branch information...
tj committed Oct 10, 2012
2 parents fee0f0d + c2fa6cc commit c492cde0488a327c8a163f30121db4ce8caa7c65
View
@@ -2,7 +2,7 @@
* Module dependencies.
*/
-var express = require('../../lib/express')
+var express = require('../..')
, hash = require('./pass').hash;
var app = module.exports = express();
@@ -49,6 +49,7 @@ hash('foobar', function(err, salt, hash){
// Authenticate using our plain-object database of doom!
+
function authenticate(name, pass, fn) {
if (!module.parent) console.log('authenticating %s:%s', name, pass);
var user = users[name];
@@ -78,7 +79,7 @@ app.get('/', function(req, res){
});
app.get('/restricted', restrict, function(req, res){
- res.send('Wahoo! restricted area');
+ res.send('Wahoo! restricted area, click to <a href="/logout">logout</a>');
});
app.get('/logout', function(req, res){
@@ -105,7 +106,7 @@ app.post('/login', function(req, res){
req.session.user = user;
req.session.success = 'Authenticated as ' + user.name
+ ' click to <a href="/logout">logout</a>. '
- + ' You may now access <a href="/restricted">/restricted</a>.';
+ + ' You may now access <a href="/restricted">/restricted</a>.';
res.redirect('back');
});
} else {
@@ -120,4 +121,4 @@ app.post('/login', function(req, res){
if (!module.parent) {
app.listen(3000);
console.log('Express started on port 3000');
-}
+}
@@ -0,0 +1,2 @@
+ </body>
+</html>
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
- <title>Authentication Example</title>
+ <title><%= title %></title>
<style>
body {
padding: 50px;
@@ -16,6 +16,3 @@
</style>
</head>
<body>
- <%- body %>
- </body>
-</html>
@@ -1,3 +1,7 @@
+
+<% var title = 'Authentication Example' %>
+<% include head %>
+
<h1>Login</h1>
<%- message %>
Try accessing <a href="/restricted">/restricted</a>, then authenticate with "tj" and "foobar".
@@ -13,4 +17,6 @@ Try accessing <a href="/restricted">/restricted</a>, then authenticate with "tj"
<p>
<input type="submit" value="Login">
</p>
-</form>
+</form>
+
+<% include foot %>
View
@@ -31,16 +31,20 @@ app.set('view engine', 'html');
// Dummy users
var users = [
- { name: 'tobi', email: 'tobi@learnboost.com' }
- , { name: 'loki', email: 'loki@learnboost.com' }
- , { name: 'jane', email: 'jane@learnboost.com' }
+ { name: 'tobi', email: 'tobi@learnboost.com' },
+ { name: 'loki', email: 'loki@learnboost.com' },
+ { name: 'jane', email: 'jane@learnboost.com' }
];
app.get('/', function(req, res){
- res.render('users', { users: users });
+ res.render('users', {
+ users: users,
+ title: "EJS example",
+ header: "Some users"
+ });
});
if (!module.parent) {
app.listen(3000);
console.log('Express app started on port 3000');
-}
+}
@@ -0,0 +1,2 @@
+</body>
+</html>
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title> <%= title %> </title>
+ <style type="text/css">
+ body {
+ padding: 50px;
+ font: 13px Helvetica, Arial, sans-serif;
+ }
+ </style>
+</head>
+<body>
@@ -1,6 +1,10 @@
+<% include header.html %>
+
<h1>Users</h1>
<ul id="users">
<% users.forEach(function(user){ %>
- <li><%= user.name %> <%= user.email %></li>
+ <li><%= user.name %> &lt;<%= user.email %>&gt;</li>
<% }) %>
-</ul>
+</ul>
+
+<% include footer.html %>
@@ -1,3 +1,4 @@
body {
padding: 50px 80px;
- font: 14px "Helvetica Nueue", "Lucida Grande", Arial, sans-serif;}
+ font: 14px "Helvetica Nueue", "Lucida Grande", Arial, sans-serif;
+}
@@ -11,9 +11,11 @@ var express = require('../..')
// Config
-app.set('view engine', 'ejs');
+app.set('view engine', 'jade');
app.set('views', __dirname + '/views');
+app.use(express.logger('dev'));
app.use(express.cookieParser());
+app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.static(__dirname + '/public'));
@@ -35,4 +37,4 @@ app.put('/user/:id/edit', user.update);
app.get('/posts', post.list);
app.listen(3000);
-console.log('Express app started on port 3000');
+console.log('Express app started on port 3000');
@@ -2,9 +2,9 @@
// 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' }
+ { 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){
@@ -11,10 +11,10 @@ a.edit {
opacity: .3;
}
a.edit::before {
- content: '[ ';
+ content: ' [';
}
a.edit::after {
- content: ' ]';
+ content: ']';
}
dt {
font-weight: bold;
@@ -2,8 +2,8 @@
// Fake user database
var users = [
- { name: 'TJ', email: 'tj@vision-media.ca' }
- , { name: 'Tobi', email: 'tobi@vision-media.ca' }
+ { name: 'TJ', email: 'tj@vision-media.ca' },
+ { name: 'Tobi', email: 'tobi@vision-media.ca' }
];
exports.list = function(req, res){
@@ -22,15 +22,15 @@ exports.load = function(req, res, next){
exports.view = function(req, res){
res.render('users/view', {
- title: 'Viewing user ' + req.user.name
- , user: req.user
+ title: 'Viewing user ' + req.user.name,
+ user: req.user
});
};
exports.edit = function(req, res){
res.render('users/edit', {
- title: 'Editing user ' + req.user.name
- , user: req.user
+ title: 'Editing user ' + req.user.name,
+ user: req.user
});
};
@@ -1,4 +0,0 @@
-<ul>
- <li>Visit the <a href="/users">users</a> page</li>
- <li>Visit the <a href="/posts">posts</a> page</li>
-</ul>
@@ -0,0 +1,6 @@
+extends layout
+
+block content
+ ul
+ li Visit the <a href="/users">users</a> page
+ li Visit the <a href="/posts">posts</a> page
@@ -1,9 +0,0 @@
-<html>
- <head>
- <title><%= title %></title>
- <link href="/style.css" rel="stylesheet" />
- </head>
- <body>
- <%- body %>
- </body>
-</html>
@@ -0,0 +1,6 @@
+html
+ head
+ title= title
+ link(href="/style.css", rel="stylesheet")
+ body
+ block content
@@ -1,7 +0,0 @@
-<h1>Posts</h1>
-<dl id="posts">
- <% posts.forEach(function(post){ %>
- <dt><%= post.title %></dt>
- <dd><%= post.body %></dd>
- <% }) %>
-</dl>
@@ -0,0 +1,8 @@
+extends ../layout
+
+block content
+ h1 Posts
+ dl#posts
+ for post in posts
+ dt= post.title
+ dd= post.body
@@ -1,9 +0,0 @@
-<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>
@@ -0,0 +1,13 @@
+extends ../layout
+
+block content
+ h1 Editing #{user.name}
+ #user
+ form(method="post")
+ input(type="hidden", value="put", name="_method")
+ p Name:
+ input(type="text", value= user.name, name="user[name]")
+ p Email:
+ input(type="text", value= user.email, name="user[email]")
+ p
+ input(type="submit", value="Save")
@@ -1,9 +0,0 @@
-<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>
@@ -0,0 +1,9 @@
+extends ../layout
+
+block content
+ h1 Users
+ #users
+ for user, i in users
+ li
+ a(href="/user/#{i}")= user.name
+ a.edit(href="/user/#{i}/edit") edit
@@ -1,4 +0,0 @@
-<h1><%= user.name %></h1>
-<div id="user">
- <p>Email: <%= user.email %></p>
-</div>
@@ -0,0 +1,6 @@
+extends ../layout
+
+block content
+ h1= user.name
+ #user
+ p Email: #{user.email}
View
@@ -1,4 +1,8 @@
+// first:
+// $ npm install redis
+// $ redis-server
+
/**
* Module dependencies.
*/
@@ -54,4 +58,4 @@ app.get('/client.js', function(req, res){
});
app.listen(3000);
-console.log('app listening on port 3000');
+console.log('app listening on port 3000');
@@ -1,4 +1,8 @@
+// first:
+// $ npm install redis
+// $ redis-server
+
var express = require('../..');
var app = express();
@@ -25,4 +29,4 @@ app.get('/', function(req, res){
});
app.listen(3000);
-console.log('Express app started on port 3000');
+console.log('Express app started on port 3000');
View
@@ -10,9 +10,9 @@ describe('ejs', function(){
.end(function(err, res){
res.should.have.status(200);
res.should.have.header('Content-Type', 'text/html; charset=utf-8');
- res.text.should.include('<li>tobi tobi@learnboost.com</li>');
- res.text.should.include('<li>loki loki@learnboost.com</li>');
- res.text.should.include('<li>jane jane@learnboost.com</li>');
+ res.text.should.include('<li>tobi &lt;tobi@learnboost.com&gt;</li>');
+ res.text.should.include('<li>loki &lt;loki@learnboost.com&gt;</li>');
+ res.text.should.include('<li>jane &lt;jane@learnboost.com&gt;</li>');
done();
});
})

0 comments on commit c492cde

Please sign in to comment.