Permalink
Browse files

Added project files

  • Loading branch information...
1 parent 7d71b4a commit ab7a886066449a4a4feec2f71d0278fbf57fd70b @hacksparrow committed Mar 25, 2012
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -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
View
52 app.js
@@ -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);
View
Binary file not shown.
View
@@ -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"
+ }
+}
View
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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;
+}
@@ -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
View
@@ -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] });
+};
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -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
View
@@ -0,0 +1,6 @@
+!!!
+html
+ head
+ title= title
+ link(rel='stylesheet', href='/stylesheets/style.css')
+ body!= body
View
@@ -0,0 +1,8 @@
+#container
+ #logo
+ a(href='/')
+ img(src='/images/logo.png')
+ #display
+ p!= content
+
+ include footer
View
@@ -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.