Created
July 21, 2012 03:44
-
-
Save darrenderidder/3154506 to your computer and use it in GitHub Desktop.
Express App outline for SHAD
This file contains hidden or 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
var express = require('express'); | |
var app = module.exports = express.createServer(); | |
// Configuration | |
app.configure(function(){ | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'ejs'); | |
app.use(express.bodyParser()); | |
app.use(express.methodOverride()); | |
app.use(express.cookieParser()); | |
app.use(express.session({ secret: 'your secret here' })); | |
app.use(app.router); | |
app.use(express.static(__dirname + '/public')); | |
}); | |
app.configure('development', function(){ | |
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); | |
}); | |
app.configure('production', function(){ | |
app.use(express.errorHandler()); | |
}); | |
// Data for our web app | |
var myData = { | |
users: [ | |
{ name: 'darren', email: '[email protected]'}, | |
{ name: 'totoro', email: '[email protected]'} | |
] | |
}; | |
// Routes | |
app.get('/', function(req, res){ | |
res.render('index', { title: 'Express' }) | |
}); | |
app.get('/users', function (req, res) { | |
res.render('users', { title: 'Express', users: myData.users}); | |
}); | |
app.post('/delete/user', function (req, res) { | |
var username = req.param('username'); | |
for (var index in myData.users) { | |
if (myData.users[index].name === username) { | |
delete myData.users[index]; | |
} | |
} | |
res.redirect('/users'); | |
}); | |
app.post('/add/user', function (req, res) { | |
var username = req.param('username'); | |
var email = req.param('email'); | |
myData.users.push({ | |
'name': username, | |
'email': email | |
}); | |
res.redirect('/users'); | |
}); | |
app.listen(3000, function(){ | |
console.log("Express server listening on port %d in %s mode", app.address().port, app. | |
settings.env); | |
}); |
This file contains hidden or 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
<h1>Users</h1> | |
<ul id="users"> | |
<% users.forEach(function (user) { %> | |
<li><%= user.name %> <%= user.email %></li> | |
<% }) %> | |
</ul> | |
<h2>Remove a user</h2> | |
<form method="post" action="/delete/user"> | |
<label>Name: </label><input name="username"></input> | |
<input type="submit"></input> | |
</form> | |
<h2>Add a user</h2> | |
<form method="post" action="/add/user"> | |
<label>Name: </label><input name="username"></input> | |
<label>Email: </label><input name="email"></input> | |
<input type="submit"></input> | |
</form> | |
~ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment