This project need a real re-thought. Currently it does too many things and needs to be splitted in many parts. I firmly believe that a proper JSON API server in order to be maintainable and easy to use needs to leave all the part of HTML generation outside its core. Trying to deal with content negotiation at the API level brings lots of extra complexity to your code and break the single responsability principle in many ways, which was one of the fundamental reasons why I started writing CoffeeStack in first place.
Node.js framework for web sites and web services based on a Domain model.
- Rapid Application Development for model based webservices in top of web sites (Single backend code to make API and web site)
- Achieve separation of concerns and code responsibilities with layered architecture (stop dumping code into the controller!)
- Using
MongoDB
as main data/session store and plugin other sources if you need to (file storage, NoSQL and Relational can live together) - Create unit and functional testable applications (make testing your app fun)
- Validate inputs against the Domain Model (stop copy and paste rules into external validators or controllers, creating ONLY valid objects to pass down to the stack)
- Log errors, info and debug separately using
bunyan
- environment based configuration file -> development, staging, testing, production
- controllers -> basic authentication check and input validation
- models -> define the domain of the application
- db mappers -> map DB data <-> model data
- web service mappers -> map model DATA <-> request specific DATA
- decorators -> add custom format to data before they render on HTML
- repositories -> abstract calls to storages and map data usign db mappers
- sources -> technology specific API and data access
- two step view -> use HTTP/1.1
Accept
to switch response output - views ->
mustache
and template engine express
-> provide middleware layer to route and dispatch requests- unit test suite using
mocha
andshould
- functional test suite using ciao
Download the code to create an initial web application setup and extend it as you wish.
git clone git@github.com:fabriziomoscon/coffeestack.git
sudo npm install -g ciao
sudo npm install -g mocha
sudo npm install -g coffee-script
sudo npm install -g bunyan
From the command line:
npm install
[sudo] npm install -g supervisor
npm run supervisor | bunyan
npm test
install ciao globally
sudo npm install ciao -g
load fixtures
curl -XPOST http://localhost:4000/testing/drop
curl -XPOST http://localhost:4000/testing/fixtures
curl -XPOST http://localhost:4000/testing/fixtures/users
run the test
console1
npm run testing | bunyan
console2 (JSON)
ciao test/functional/json -c test/functional/ciao.json
console3 (HTML)
ciao test/functional/html -c test/functional/ciao.json
In order to run nodejs on lower port without root permission the following has been installed and executed
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep /usr/bin/nodejs
sudo setcap cap_net_bind_service=+ep /usr/local/lib/node_modules/coffee-script/bin/coffee
In order to switch between different versions of node we installed nave which can be used easly with the following
sudo nave usemain 0.10
Start the server as a daemon:
nohup npm run supervisor >> /var/www/app.log 2>&1 &
You can access collections and documents inside the database installing smog on your local machine
[sudo] npm install smog -g
and executing it
smog &
once smog is running open a browser to http://localhost:8080/ and change test
to coffeestackDB
inside the DSN popup
NOTE
NODE_PATH='./:$NODE_PATH' and NODE_ENV='staging' must be in ~/.bashrc or exported in the current env
The front-end code is using Jake as a build tool.
Jake is installed by npm as one of the dev dependencies.
Jake can execute tasks from Jakefile.coffee
.
Run jake -T
to see a list of the available tasks.
Run jake <task name>
to execute a task.
When a task depends on some other tasks,
those other tasks will be executed first.
CSS is compiled from Stylus code. The Stylus middleware automatically recompiles when changes are made.
There is a watch
Jake task
that can be used to automatically
compile and bundle front-end CoffeeScript
when it is changed.