Permalink
Browse files

Merge branch 'master' into 1.x

  • Loading branch information...
2 parents 569c513 + 5091c9e commit e1d33992a7f26108e07a9dd06ff5ff296052d9d2 @tj tj committed Nov 13, 2010
View
73 docs/guide.md
@@ -42,19 +42,19 @@ Note the use of _app.router_, which can (optionally) be used to mount the applic
otherwise the first call to _app.{get,put,del,post}()_ will mount the routes.
app.configure(function(){
- app.use(express.methodOverride());
- app.use(express.bodyDecoder());
- app.use(app.router);
- app.use(express.staticProvider(__dirname + '/public'));
- });
+ app.use(express.methodOverride());
+ app.use(express.bodyDecoder());
+ app.use(app.router);
+ app.use(express.staticProvider(__dirname + '/public'));
+ });
- app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
- });
+ app.configure('development', function(){
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ });
- app.configure('production', function(){
- app.use(express.errorHandler());
- });
+ app.configure('production', function(){
+ app.use(express.errorHandler());
+ });
For internal and arbitrary settings Express provides the _set(key[, val])_, _enable(key)_, _disable(key)_ methods:
@@ -96,8 +96,8 @@ can be done by defining the route below. The values associated to the named plac
are available as `req.params`.
app.get('/user/:id', function(req, res){
- res.send('user ' + req.params.id);
- });
+ res.send('user ' + req.params.id);
+ });
A route is simple a string which is compiled to a _RegExp_ internally. For example
when _/user/:id_ is compiled, a simplified version of the regexp may look similar to:
@@ -153,6 +153,19 @@ may consume:
/products.xml
/products
+For example we can __POST__ some json, and echo the json back using the _bodyDecoder_ middleware which will parse json request bodies (as well as others), and place the result in _req.body_:
+
+ var express = require('express')
+ , app = express.createServer();
+
+ app.use(express.bodyDecoder());
+
+ app.post('/', function(req, res){
+ res.send(req.body);
+ });
+
+ app.listen(3000);
+
### Passing Route Control
We may pass control to the next _matching_ route, by calling the _third_ argument,
@@ -172,6 +185,40 @@ and middleware continue to be invoked. The same is true for several routes which
// do something else
});
+Express 1.0 also introduces the _all()_ method, which provides a route callback matching any HTTP method. This is useful in many ways, one example being the loading of resources before executing subsequent routes as shown below:
+
+ var express = require('express')
+ , app = express.createServer();
+
+ var users = [{ name: 'tj' }];
+
+ app.all('/user/:id/:op?', function(req, res, next){
+ req.user = users[req.params.id];
+ if (req.user) {
+ next();
+ } else {
+ next(new Error('cannot find user ' + req.params.id));
+ }
+ });
+
+ app.get('/user/:id', function(req, res){
+ res.send('viewing ' + req.user.name);
+ });
+
+ app.get('/user/:id/edit', function(req, res){
+ res.send('editing ' + req.user.name);
+ });
+
+ app.put('/user/:id', function(req, res){
+ res.send('updating ' + req.user.name);
+ });
+
+ app.get('*', function(req, res){
+ res.send('what???', 404);
+ });
+
+ app.listen(3000);
+
### Middleware
Middleware via [Connect](http://github.com/senchalabs/connect) can be
View
4 examples/github/public/style.css
@@ -1,6 +1,6 @@
body {
padding: 30px 50px;
- font: 12px/1.4 "Lucida Grande", "Helvetica Nueue", Arial, sans-serif;
+ font: 12px/1.4 "Helvetica Neue", Arial, sans-serif;
}
a {
color: #00AAFF;
@@ -12,7 +12,7 @@ a:hover {
.user {
margin: 0 10px;
float: left;
- width: 30%;
+ width: 25%;
}
table td:nth-child(2) {
padding: 0 5px;
View
41 examples/route-separation/app.js
@@ -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');
View
17 examples/route-separation/post.js
@@ -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
24 examples/route-separation/public/style.css
@@ -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;
+}
View
8 examples/route-separation/site.js
@@ -0,0 +1,8 @@
+
+exports.index = function(req, res){
+ res.render('index', {
+ locals: {
+ title: 'Route Separation Example'
+ }
+ });
+};
View
53 examples/route-separation/user.js
@@ -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');
+};
View
4 examples/route-separation/views/index.ejs
@@ -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>
View
9 examples/route-separation/views/layout.ejs
@@ -0,0 +1,9 @@
+<html>
+ <head>
+ <title><%= title %></title>
+ <link href="/style.css" rel="stylesheet" />
+ </head>
+ <body>
+ <%- body %>
+ </body>
+</html>
View
7 examples/route-separation/views/post/list.ejs
@@ -0,0 +1,7 @@
+<h1>Posts</h1>
+<dl id="posts">
+ <% posts.forEach(function(post){ %>
+ <dt><%= post.title %></dt>
+ <dd><%= post.body %></dd>
+ <% }) %>
+</dl>
View
9 examples/route-separation/views/user/edit.ejs
@@ -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>
View
9 examples/route-separation/views/user/list.ejs
@@ -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>
View
4 examples/route-separation/views/user/view.ejs
@@ -0,0 +1,4 @@
+<h1><%= user.name %></h1>
+<div id="user">
+ <p>Email: <%= user.email %></p>
+</div>
View
3 examples/session/app.js
@@ -29,4 +29,5 @@ app.get('/', function(req, res){
res.send(body + '<p>viewed <strong>' + req.session.views + '</strong> times.</p>');
});
-app.listen(3000);
+app.listen(3000);
+console.log('Express app started on port 3000');
View
2 package.json
@@ -9,7 +9,7 @@
{ "name": "Ciaran Jessup", "email": "ciaranj@gmail.com" },
{ "name": "Guillermo Rauch", "email": "rauchg@gmail.com" }
],
- "dependencies": { "connect": ">= 0.2.7" },
+ "dependencies": { "connect": ">= 0.3.0" },
"keywords": ["framework", "sinatra", "web", "rest", "restful"],
"directories": { "lib": "./lib/express" },
"scripts": { "test": "make test" },

0 comments on commit e1d3399

Please sign in to comment.