Bookstore is an online book sales system.
Bookstore was produced as part of a univeristy assignment. I make no guarantees about quality or completeness.
## Installation Guide #### Conventions {bracketed_items} are variable and will change based on deployment.{server_domain} = bookstoredomain.com
{server_name} = bookstoredomain
$ ssh-keygen -f ~/.ssh/my_key
-
send key to server (on client)
$ ssh-copy-id -i ~/.ssh/my_key.pub root@{server_domain}
-
ssh to server
$ ssh root@{server_domain}
-
add node.js PPA
$ sudo apt-get install python-software properties $ sudo add-apt-repository ppa:chris-lea/node.js
-
add MongoDB repository
$ sudo echo "deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen" >> /etc/apt/sources.list $ apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10
-
update and install software
$ sudo apt-get update $ sudo apt-get upgrade $ sudo apt-get install nginx node npm fail2ban mongodb-10gen
-
create user for bookstore
$ sudo adduser \ > --system \ > --shell /bin/bash \ > --gecos 'user for bookstore' \ > --group \ > --disabled-password \ > --home /home/bookstore \ > bookstore
-
install bookstore software
$ cd /home/bookstore $ git clone https://github.com/rozifus/bookstore-uni-assignment.git app $ cd app $ npm install
-
configure bookstore domain
/home/bookstore/app/app.js
... var server_domain = {server_domain} ...
-
create bookstore upstart profile
/etc/init/bookstore.conf
#!upstart description "bookshop service" start on filesystem and static-network-up stop on shutdown script export HOME="/home/bookstore" echo $$ > /var/run/bookstore.pid exec sudo -u bookstore /usr/bin/node /home/bookstore/app/app.js >> /var/log/bookstore.pid 2>&1 end script
-
create nginx config
/etc/nginx/sites-available/{server_name}
upstream app_bookstore { server 127.0.0.1:8001; } server { listen 0.0.0.0:80; server_name {server_domain}; access_log /var/log/nginx/{server_name}.log; location / { proxy_set_header X-Real_IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-NginX-Proxy true; proxy_pass http://app_bookstore/; proxy_redirect off; } }
-
enable nginx site
$ sudo ln -s /etc/nginx/sites-available/{server_name} /etc/nginx/sites-enabled/{server_name}
git clone https://github.com/rozifus/bookstore-uni-assignment.git
- node.js
0.8.9
- node package manager (npm)
1.1.59
- Node Packages
- async
0.1.22
- express
2.5.8
- express-validator
0.2.4
- jade
0.27.2
- mocha
1.6.0
- mongoose
3.0.1
- mongoose-validator
0.1.3
- should
1.2.0
- validator
0.4.12
- async
- Other Software
- twitter boostrap
2.1.1
- jQuery
1.8.2
- twitter boostrap
.
├── app_modules - contains mounted express applications
│ ├── admin - the admin module
│ │ ├── public - static files
│ │ ├── routes - routing / control-logic
│ │ ├── views - layouts for rendering
│ │ ├── app.js - admin module express application
│ │ └── app_middleware.js - admin module custom middleware
│ ├── bookstore - the bookstore module
│ │ ├── public - static files
│ │ ├── routes - routing / control-logic
│ │ ├── views - templates for rendering
│ │ │ ├── account - account related templates
│ │ │ ├── checkout - checkout related templates
│ │ ├── app.js - bookstore module express application
│ │ └── app_middleware.js - bookstore module customer middleware
│ └── staff - the staff module
│ ├── public - static files
│ ├── routes - routing / control-logic
│ ├── views - layouts for rendering
│ ├── app.js - staff module express application
│ └── app_middleware.js - staff module custom middleware
├── models - mongoose data models
├── test - test suites
│ ├── config.js - test configuration data
│ ├── detect.js - error detectors for tests
│ ├── generate.js - field generators for tests
│ ├── test.database.js - database test suite
│ ├── test.model-customer.js - customer model test suite
│ ├── test.model-staff.js - staff model test suite
│ └── valid.js - known valid data for testing
├── app.js - main application
├── db-connect.js - database init
├── Makefile - alias' for tests
├── package.json - package information for npm
└── Readme.md - the file you are viewing currently
$ npm install
-
run tests
- condensed report
$ make test
- complete report
$ make test-verbose
var myfunc = function(callback) { //this happens first.. callback() //this happens last.. };
myfunc(paramfunc);
Often we won't define our callback before hand, we'll instead create it as an anonymous function within the parameters of the calling function:
```javascript
var myfunc = function(callback) {
//this happens first..
callback()
//this happens last..
};
myfunc(function() { //this happens second.. } );
We might also open up the anonymous callback function so that we can place code inside.
var myfunc = function(callback) {
//this happens first..
callback()
//this happens last..
};
myfunc(function() {
//this happens second..
});
This allows us to do asynchronous tasks safely as we can use these callbacks to delay sensitive operations unit it's safe for them to run:
var saveToDatabase = function(data, callback) {
//save our data, happens first..
callback()
//this happens last..
};
saveToDatabase(mydata, function() {
//use our saved data, happens second..
});
We also might pass an error back into our callback if something goes wrong:
var saveToDatabase = function(data, callback) {
//save our data, happens first..
if (something_went_wrong) {
callback(error);
} else {
callback(null);
};
//this happens last..
};
saveToDatabase(mydata, function(error) {
if (error) {
//deal with the error
} else {
//use our saved data, happens second..
};
});
These types of callbacks will appear frequently with the application source.
#### Jade TemplatesThe views for the app were created in the Jade templating language.
Jade renders to html but uses indentation rather than tags:
Jade
html
head
title My Site
body
p Some Text
html
<html>
<head>
<title>My Site</title>
</head>
<body>
<p>Some Text</p>
</body>
</html>
Jade also uses the .
character to denote classes, #
to denote id's and parenthesis ()
for other attributes:
Jade
span.myclass some text
span#myid some text
span(myattr="myvalue") some text
html
<span class="myclass">some text</span>
<span id="myid">some text</span>
<span myattr="myvalue">some text</span>
(The MIT License)
Copyright (c) 2012 Ryan Miller <rozifus@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.