mirror of
https://github.com/zebrajr/express.git
synced 2025-12-06 00:19:48 +01:00
remove mvc example
too large to actually be helpful... and annoying to maintain :)
This commit is contained in:
parent
b55eb5acf5
commit
cc69d50c60
|
|
@ -1,13 +0,0 @@
|
|||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('../../lib/express');
|
||||
|
||||
var app = express.createServer();
|
||||
|
||||
require('./mvc').boot(app);
|
||||
|
||||
app.listen(3000);
|
||||
console.log('Express app started on port 3000');
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
|
||||
module.exports = {
|
||||
|
||||
// /
|
||||
|
||||
index: function(req, res){
|
||||
res.render();
|
||||
}
|
||||
};
|
||||
|
|
@ -1,55 +0,0 @@
|
|||
|
||||
// Fake user database for example
|
||||
|
||||
var users = [
|
||||
{ id: 0, name: 'TJ', email: 'tj@vision-media.ca' }
|
||||
, { id: 1, name: 'Simon', email: 'simon@vision-media.ca' }
|
||||
];
|
||||
|
||||
function get(id, fn) {
|
||||
if (users[id]) {
|
||||
fn(null, users[id]);
|
||||
} else {
|
||||
fn(new Error('User ' + id + ' does not exist'));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
// /users
|
||||
|
||||
index: function(req, res){
|
||||
res.render(users);
|
||||
},
|
||||
|
||||
// /users/:id
|
||||
|
||||
show: function(req, res, next){
|
||||
get(req.params.id, function(err, user){
|
||||
if (err) return next(err);
|
||||
res.render(user);
|
||||
});
|
||||
},
|
||||
|
||||
// /users/:id/edit
|
||||
|
||||
edit: function(req, res, next){
|
||||
get(req.params.id, function(err, user){
|
||||
if (err) return next(err);
|
||||
res.render(user);
|
||||
});
|
||||
},
|
||||
|
||||
// PUT /users/:id
|
||||
|
||||
update: function(req, res, next){
|
||||
var id = req.params.id;
|
||||
get(id, function(err){
|
||||
if (err) return next(err);
|
||||
var user = users[id] = req.body.user;
|
||||
user.id = id;
|
||||
req.flash('info', 'Successfully updated _' + user.name + '_.');
|
||||
res.redirect('back');
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -1,148 +0,0 @@
|
|||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var fs = require('fs')
|
||||
, express = require('../../lib/express');
|
||||
|
||||
exports.boot = function(app){
|
||||
bootApplication(app);
|
||||
bootControllers(app);
|
||||
};
|
||||
|
||||
// App settings and middleware
|
||||
|
||||
function bootApplication(app) {
|
||||
app.use(express.logger(':method :url :status'));
|
||||
app.use(express.bodyParser());
|
||||
app.use(express.methodOverride());
|
||||
app.use(express.cookieParser());
|
||||
app.use(express.session({ secret: 'keyboard cat' }));
|
||||
app.use(app.router);
|
||||
app.use(express.static(__dirname + '/public'));
|
||||
|
||||
// Example 500 page
|
||||
app.use(function(err, req, res, next){
|
||||
res.render('500');
|
||||
});
|
||||
|
||||
// Example 404 page via simple Connect middleware
|
||||
app.use(function(req, res){
|
||||
res.render('404');
|
||||
});
|
||||
|
||||
// Setup ejs views as default, with .html as the extension
|
||||
app.set('views', __dirname + '/views');
|
||||
app.register('.html', require('ejs'));
|
||||
app.set('view engine', 'html');
|
||||
|
||||
// Some dynamic view helpers
|
||||
app.dynamicHelpers({
|
||||
request: function(req){
|
||||
return req;
|
||||
},
|
||||
|
||||
hasMessages: function(req){
|
||||
if (!req.session) return false;
|
||||
return Object.keys(req.session.flash || {}).length;
|
||||
},
|
||||
|
||||
messages: function(req){
|
||||
return function(){
|
||||
var msgs = req.flash();
|
||||
return Object.keys(msgs).reduce(function(arr, type){
|
||||
return arr.concat(msgs[type]);
|
||||
}, []);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Bootstrap controllers
|
||||
|
||||
function bootControllers(app) {
|
||||
fs.readdir(__dirname + '/controllers', function(err, files){
|
||||
if (err) throw err;
|
||||
files.forEach(function(file){
|
||||
bootController(app, file);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Example (simplistic) controller support
|
||||
|
||||
function bootController(app, file) {
|
||||
var name = file.replace('.js', '')
|
||||
, actions = require('./controllers/' + name)
|
||||
, plural = name + 's' // realistically we would use an inflection lib
|
||||
, prefix = '/' + plural;
|
||||
|
||||
// Special case for "app"
|
||||
if (name == 'app') prefix = '/';
|
||||
|
||||
Object.keys(actions).map(function(action){
|
||||
var fn = controllerAction(name, plural, action, actions[action]);
|
||||
switch(action) {
|
||||
case 'index':
|
||||
app.get(prefix, fn);
|
||||
break;
|
||||
case 'show':
|
||||
app.get(prefix + '/:id.:format?', fn);
|
||||
break;
|
||||
case 'add':
|
||||
app.get(prefix + '/:id/add', fn);
|
||||
break;
|
||||
case 'create':
|
||||
app.post(prefix + '/:id', fn);
|
||||
break;
|
||||
case 'edit':
|
||||
app.get(prefix + '/:id/edit', fn);
|
||||
break;
|
||||
case 'update':
|
||||
app.put(prefix + '/:id', fn);
|
||||
break;
|
||||
case 'destroy':
|
||||
app.del(prefix + '/:id', fn);
|
||||
break;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Proxy res.render() to add some magic
|
||||
|
||||
function controllerAction(name, plural, action, fn) {
|
||||
return function(req, res, next){
|
||||
var render = res.render
|
||||
, format = req.params.format
|
||||
, path = __dirname + '/views/' + name + '/' + action + '.html';
|
||||
res.render = function(obj, options, fn){
|
||||
res.render = render;
|
||||
// Template path
|
||||
if (typeof obj === 'string') {
|
||||
return res.render(obj, options, fn);
|
||||
}
|
||||
|
||||
// Format support
|
||||
if (action == 'show' && format) {
|
||||
if (format === 'json') {
|
||||
return res.send(obj);
|
||||
} else {
|
||||
throw new Error('unsupported format "' + format + '"');
|
||||
}
|
||||
}
|
||||
|
||||
// Render template
|
||||
res.render = render;
|
||||
options = options || {};
|
||||
// Expose obj as the "users" or "user" local
|
||||
if (action == 'index') {
|
||||
options[plural] = obj;
|
||||
} else {
|
||||
options[name] = obj;
|
||||
}
|
||||
return res.render(path, options, fn);
|
||||
};
|
||||
fn.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
|
@ -1,24 +0,0 @@
|
|||
body {
|
||||
padding: 30px;
|
||||
font: 12px/1.4 "Helvetica Nueue", "Lucida Grande", Arial, sans-serif;
|
||||
}
|
||||
a {
|
||||
color: #00aaff;
|
||||
text-decoration: none;
|
||||
}
|
||||
a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
#messages {
|
||||
width: 95%;
|
||||
margin-bottom: 15px;
|
||||
padding: 10px;
|
||||
border: 1px solid #00DD00;
|
||||
-webkit-box-shadow: 1px 1px 4px rgba(0,0,0,0.2);
|
||||
}
|
||||
#messages li {
|
||||
list-style: none;
|
||||
}
|
||||
#messages em {
|
||||
font-weight: bold;
|
||||
}
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
<h1>Cannot find <%= request.url %></h1>
|
||||
<p>Sorry we failed to locate this page or resource.</p>
|
||||
|
|
@ -1,2 +0,0 @@
|
|||
<h1>Internal Server Error</h1>
|
||||
<p>Sorry an error has occurred, please try refreshing the page or navigate back to <a href="/">home</a>.</p>
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
<h1>Application Index</h1>
|
||||
<p>Try visiting one of the following urls:</p>
|
||||
<ul>
|
||||
<li><a href="/users">/users</a></li>
|
||||
<li><a href="/users/1">/users/1</a></li>
|
||||
<li><a href="/users/1.json">/users/1.json</a></li>
|
||||
<li><a href="/users/1/edit">/users/1/edit</a></li>
|
||||
<li><a href="/accounts">/accounts (404, non existent)</a></li>
|
||||
<li><a href="/users/1.foo">/users/1.foo (500, unsupported format)</a></li>
|
||||
</ul>
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Express - MVC Example</title>
|
||||
<link rel="stylesheet" href="/style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<%- body %>
|
||||
</body>
|
||||
</html>
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<% if (hasMessages) { %>
|
||||
<ul id="messages">
|
||||
<% messages().forEach(function(msg){ %>
|
||||
<li><%- msg %></li>
|
||||
<% }) %>
|
||||
</ul>
|
||||
<% } %>
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
<h1>Editing user <%= user.name %></h1>
|
||||
<%- partial('../messages') %>
|
||||
<form method="post" action="/users/<%= user.id %>">
|
||||
<input type="hidden" name="_method" value="put" />
|
||||
<p>Name: <input type="text" name="user[name]", value="<%= user.name %>" /></p>
|
||||
<p>Email: <input type="text" name="user[email]", value="<%= user.email %>" /></p>
|
||||
<p><input type="submit" value="Update" /></p>
|
||||
</form>
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<h1>Users</h1>
|
||||
<%- partial('../messages') %>
|
||||
<ul>
|
||||
<% users.forEach(function(user){ %>
|
||||
<li><a href="/users/<%= user.id %>"><%= user.name %></a></li>
|
||||
<% }) %>
|
||||
</ul>
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
<h1>Viewing user #<%= user.id %></h1>
|
||||
<%- partial('../messages') %>
|
||||
<ul>
|
||||
<li><%- user.name %></li>
|
||||
<li><%- user.email %></li>
|
||||
<li><a href="<%= user.id %>/edit">Edit</a> this user</li>
|
||||
</ul>
|
||||
Loading…
Reference in New Issue
Block a user