Skip to content

Commit

Permalink
Added project files
Browse files Browse the repository at this point in the history
  • Loading branch information
hacksparrow committed Mar 25, 2012
1 parent 7d71b4a commit ab7a886
Show file tree
Hide file tree
Showing 16 changed files with 234 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
NINJA STORE
===========
-----------
Ninja Store is a very simple Express.js app for you to hack around and uderstand Express better.

It is a good project to start learnig Express because if covers GET and POST requests, the Jade template engine, the Stylus CSS engine, and sessions. All of it while being a tiny project.

-----------
Created by Captain Hack Sparrow
52 changes: 52 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

/**
* Module dependencies.
*/

var express = require('express');
var store = require('./routes/store');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'your secret here' }));
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
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());
});

// Routes

app.get('/', store.home);
app.post('/', store.home_post_handler);

// display the list of item
app.get('/items', store.items);
// show individual item
app.get('/item/:id', store.item);
// show general pages
app.get('/page', store.page);
app.get('/logout', function(req, res) {
// delete the session variable
delete req.session.username;
// redirect user to homepage
res.redirect('/');
});

app.listen(3000);
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
Binary file added ninja-store.zip
Binary file not shown.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "application-name"
, "version": "0.0.1"
, "private": true
, "dependencies": {
"express": "2.5.8"
, "stylus": ">= 0.0.1"
, "jade": ">= 0.0.1"
}
}
Binary file added public/images/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/stylesheets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
body {
padding: 50px;
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif;
background: #ccc;
}
a {
color: #0069ff;
}
#container {
width: 450px;
margin: 0 auto;
padding: 40px 20px;
background: #fff;
box-shadow: 1px 3px 3px #333;
border-radius: 5px;
}
#logo {
text-align: center;
}
#display {
margin: 20px 0 50px;
}
#userbar {
margin-bottom: 10px;
}
24 changes: 24 additions & 0 deletions public/stylesheets/style.styl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
body
padding: 50px
font: 14px "Lucida Grande", Helvetica, Arial, sans-serif
background: #ccc

a
color: #0069FF

#container
width: 450px
margin: 0 auto
padding: 40px 20px
background: #fff
box-shadow: 1px 3px 3px #333
border-radius: 5px

#logo
text-align: center

#display
margin: 20px 0 50px

#userbar
margin-bottom: 10px
54 changes: 54 additions & 0 deletions routes/store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// handler for homepage
exports.home = function(req, res) {
// if user is not logged in, ask them to login
if (typeof req.session.username == 'undefined') res.render('home', { title: 'Ninja Store'});
// if user is logged in already, take them straight to the items list
else res.redirect('/items');
};

// handler for form submitted from homepage
exports.home_post_handler = function(req, res) {
// if the username is not submitted, give it a default of "Anonymous"
username = req.body.username || 'Anonymous';
// store the username as a session variable
req.session.username = username;
// redirect the user to homepage
res.redirect('/');
};

// our 'database'
var items = {
SKN:{name:'Shuriken', price:100},
ASK:{name:'Ashiko', price:690},
CGI:{name:'Chigiriki', price:250},
NGT:{name:'Naginata', price:900},
KTN:{name:'Katana', price:1000}
};

// handler for displaying the items
exports.items = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else res.render('items', { title: 'Ninja Store - Items', username: req.session.username, items:items });
};

// handler for displaying individual items
exports.item = function(req, res) {
// don't let nameless people view the items, redirect them back to the homepage
if (typeof req.session.username == 'undefined') res.redirect('/');
else {
var name = items[req.params.id].name;
var price = items[req.params.id].price;
res.render('item', { title: 'Ninja Store - ' + name, username: req.session.username, name:name, price:price });
}
};

// handler for showing simple pages
exports.page = function(req, res) {
var name = req.query.name;
var contents = {
about: 'Ninja Store sells the coolest ninja stuff in the world. Anyone shopping here is cool.',
contact: 'You can contact us at <address><strong>Ninja Store</strong>,<br>1, World Ninja Headquarters,<br>Ninja Avenue,<br>NIN80B7-JP,<br>Nihongo.</address>'
};
res.render('page', { title: 'Ninja Store - ' + name, username: req.session.username, content:contents[name] });
};
5 changes: 5 additions & 0 deletions views/footer.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#footer
div Copyright &copy; Ninja Store #{+new Date().getFullYear()}
a(href='/page?name=about') About
| |
a(href='/page?name=contact') Contact Us
13 changes: 13 additions & 0 deletions views/home.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#container
#logo
a(href='/')
img(src='/images/logo.png')
#display
#login
form(method='post')
| Enter your name if you want to be a ninja
div
input(type='text', name='username')
input(type='submit', value='Log In')

include footer
10 changes: 10 additions & 0 deletions views/item.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#container
#logo
img(src='/images/logo.png')
#display
include userbar

p The #{name.toLowerCase()} is one of the must-have items for any aspiring ninja. It costs just $#{price} on our store.
p Buy it today!

include footer
12 changes: 12 additions & 0 deletions views/items.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#container
#logo
img(src='/images/logo.png')
#display
include userbar

-for (var id in items)
- var item = items[id]
div
a(href='/item/#{id}') #{item.name} - $#{item.price}

include footer
6 changes: 6 additions & 0 deletions views/layout.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
!!!
html
head
title= title
link(rel='stylesheet', href='/stylesheets/style.css')
body!= body
8 changes: 8 additions & 0 deletions views/page.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#container
#logo
a(href='/')
img(src='/images/logo.png')
#display
p!= content

include footer
5 changes: 5 additions & 0 deletions views/userbar.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#userbar
| Welcome #{username} |
a(href='/items') Items
| |
a(href='/logout') Log Out

0 comments on commit ab7a886

Please sign in to comment.