mirror of
https://github.com/sstent/node.git
synced 2026-01-25 14:42:00 +00:00
first post
This commit is contained in:
1
ninja-store/.gitignore
vendored
Normal file
1
ninja-store/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
node_modules/
|
||||
9
ninja-store/README.md
Normal file
9
ninja-store/README.md
Normal file
@@ -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, login-logout, and sessions. All of it while being a tiny project.
|
||||
|
||||
-----------
|
||||
Created by Captain Hack Sparrow
|
||||
52
ninja-store/app.js
Normal file
52
ninja-store/app.js
Normal file
@@ -0,0 +1,52 @@
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var express = require('express');
|
||||
var store = require('./routes/store');
|
||||
var db_helper = module.exports = require("./routes/db_helper.js");
|
||||
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);
|
||||
10
ninja-store/package.json
Normal file
10
ninja-store/package.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
BIN
ninja-store/public/images/logo.png
Normal file
BIN
ninja-store/public/images/logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.4 KiB |
25
ninja-store/public/stylesheets/style.css
Normal file
25
ninja-store/public/stylesheets/style.css
Normal file
@@ -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
ninja-store/public/stylesheets/style.styl
Normal file
24
ninja-store/public/stylesheets/style.styl
Normal file
@@ -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
|
||||
69
ninja-store/routes/db_helper.js
Normal file
69
ninja-store/routes/db_helper.js
Normal file
@@ -0,0 +1,69 @@
|
||||
var mysql = require('mysql');
|
||||
var MYSQL_USERNAME = 'root';
|
||||
var MYSQL_PASSWORD = 'condoms';
|
||||
|
||||
// init;
|
||||
var client = mysql.createClient({
|
||||
user: MYSQL_USERNAME,
|
||||
password: MYSQL_PASSWORD,
|
||||
});
|
||||
|
||||
// destroy old db
|
||||
//client.query('DROP DATABASE IF EXISTS mynode_db', function(err) {
|
||||
// if (err) { throw err; }
|
||||
//});
|
||||
|
||||
// create database
|
||||
//client.query('CREATE DATABASE mynode_db', function(err) {
|
||||
// if (err) { throw err; }
|
||||
//});
|
||||
//console.log('database mynode_db is created.');
|
||||
client.query('USE mynode_db');
|
||||
|
||||
// create table
|
||||
//var sql = ""+
|
||||
//"create table employees("+
|
||||
//" id int unsigned not null auto_increment,"+
|
||||
//" name varchar(50) not null default 'unknown',"+
|
||||
//" salary dec(10,2) not null default 100000.00,"+
|
||||
//" primary key (id)"+
|
||||
//");";
|
||||
//client.query(sql, function(err) {
|
||||
// if (err) { throw err; }
|
||||
//});
|
||||
//console.log('table employees is created.');
|
||||
|
||||
// function to create employee
|
||||
exports.add_employee = function(data, callback) {
|
||||
client.query("insert into employees (name, salary) values (?,?)", [data.name, data.salary], function(err, info) {
|
||||
// callback function returns last insert id
|
||||
callback(info.insertId);
|
||||
console.log('Employee '+data.name+' has salary '+data.salary);
|
||||
});
|
||||
}
|
||||
|
||||
// function to get list of employees
|
||||
exports.get_employees = function(callback) {
|
||||
client.query("select * from employees", function(err, results, fields) {
|
||||
// callback function returns employees array
|
||||
callback(results);
|
||||
});
|
||||
}
|
||||
|
||||
// function to get list of employees
|
||||
exports.get_all = function(data, callback) {
|
||||
console.log('Table '+data);
|
||||
client.query("select * from employees", function(err, results, fields) {
|
||||
// callback function returns employees array
|
||||
console.log('hmmmjson' + JSON.stringify(results));
|
||||
var ook= "ook"
|
||||
console.log('hmmmook' + ook);
|
||||
callback(err,results);
|
||||
//callback(results);
|
||||
});
|
||||
}
|
||||
|
||||
exports.fake = function(callback) {
|
||||
console.log('database mynode_db is created.');
|
||||
};
|
||||
|
||||
8
ninja-store/routes/index.js
Normal file
8
ninja-store/routes/index.js
Normal file
@@ -0,0 +1,8 @@
|
||||
|
||||
/*
|
||||
* GET home page.
|
||||
*/
|
||||
|
||||
exports.index = function(req, res){
|
||||
res.render('index', { title: 'Express' })
|
||||
};
|
||||
70
ninja-store/routes/store.js
Normal file
70
ninja-store/routes/store.js
Normal file
@@ -0,0 +1,70 @@
|
||||
var io = require('../app').sio;
|
||||
var db_helper = require('./db_helper');
|
||||
|
||||
|
||||
// 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) {
|
||||
var items = db_helper.get_all('employees', function(err, items) {
|
||||
if (err) {
|
||||
console.log("async: " + err);
|
||||
} else {
|
||||
console.log('hmmmt1json' + items);
|
||||
}
|
||||
console.log('hmmmt1jsoni' + JSON.stringify(items));
|
||||
// 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) {
|
||||
var test = new db_helper.get_all('employees',function(err, results, fields){
|
||||
|
||||
});
|
||||
|
||||
// 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 - ' + test[1], 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
ninja-store/views/footer.jade
Normal file
5
ninja-store/views/footer.jade
Normal file
@@ -0,0 +1,5 @@
|
||||
#footer
|
||||
div Copyright © Ninja Store #{+new Date().getFullYear()}
|
||||
a(href='/page?name=about') About
|
||||
| |
|
||||
a(href='/page?name=contact') Contact Us
|
||||
13
ninja-store/views/home.jade
Normal file
13
ninja-store/views/home.jade
Normal file
@@ -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
|
||||
2
ninja-store/views/index.jade
Normal file
2
ninja-store/views/index.jade
Normal file
@@ -0,0 +1,2 @@
|
||||
h1= title
|
||||
p Welcome to #{title}
|
||||
10
ninja-store/views/item.jade
Normal file
10
ninja-store/views/item.jade
Normal file
@@ -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
ninja-store/views/items.jade
Normal file
12
ninja-store/views/items.jade
Normal file
@@ -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
|
||||
8
ninja-store/views/layout.jade
Normal file
8
ninja-store/views/layout.jade
Normal file
@@ -0,0 +1,8 @@
|
||||
!!!
|
||||
html
|
||||
head
|
||||
title= title
|
||||
link(rel='stylesheet', href='/stylesheets/style.css')
|
||||
<script src="http://localhost:3000/socket.io/socket.io.js"></script>
|
||||
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
|
||||
body!= body
|
||||
8
ninja-store/views/page.jade
Normal file
8
ninja-store/views/page.jade
Normal file
@@ -0,0 +1,8 @@
|
||||
#container
|
||||
#logo
|
||||
a(href='/')
|
||||
img(src='/images/logo.png')
|
||||
#display
|
||||
p!= content
|
||||
|
||||
include footer
|
||||
5
ninja-store/views/userbar.jade
Normal file
5
ninja-store/views/userbar.jade
Normal file
@@ -0,0 +1,5 @@
|
||||
#userbar
|
||||
| Welcome #{username} |
|
||||
a(href='/items') Items
|
||||
| |
|
||||
a(href='/logout') Log Out
|
||||
Reference in New Issue
Block a user