Kraken.js on Nunjucks
1. Installation
- Clone the repository and checkout the folder
- Install project dependencies
- All Environments:
[sudo] npm install -g grunt-cli pm2 && npm install
- All Environments:
- Build assets:
- Development (build and watch):
grunt
- Production (build):
grunt build
- Development (build and watch):
- Run node server:
- Development environment:
[PORT=8000] node .
- Production environment:
[PORT=8000] NODE_ENV=production node .
- Development environment:
1.1. Process Manager
- You can use PM2 process manager so that the application restarts automatically in development mode and watches for file changes. It sets port and environment variables automatically via configuration file.
- Install it with
[sudo] npm install -g pm2
- Start the application:
pm2 start <development.json|production.json>
- Start or restart the application:
pm2 startOrRestart <development.json|production.json>
- Monit processes:
pm2 monit
- Tail logs:
pm2 logs
- Reload processes:
pm2 reload all
- Restart processes:
pm2 restart all
- Kill the process manager:
pm2 kill
- Install it with
2. Configuration
2.1. Node.js
Node.js configuration is stored in config directory. Production configuration is called config.json
, development overrides production and is called development.json
3. Debugging
3.1. Application Logs
- In order to see debug logs, simply prepend
DEBUG=*
to the command you're running, e.g.DEBUG=* node .
- In order to keep debug information of a given type on your screen, use proper filtering, e.g.
DEBUG=express:router
,DEBUG=express:*
etc.
3.2. JavaScript debugging
- In order to debug JavaScript like you do it with the browser:
- Install node-inspector globally with
[sudo] npm install -g node-inspector
- Open node-inspector on a given port, e.g.
node-inspector --web-port 9191
- Run your application with
--debug
flag if you'd like to set breakpoints yourself, e.g.node --debug .
- Run it with
--debug-brk
if you'd like to break before JavaScript starts executing, e.g.node --debug-brk .
- Open URL
node-inspector
provided you with in your browser, e.g.http://127.0.0.1:9191/debug?ws=127.0.0.1:9191&port=5858
- Install node-inspector globally with
4. Dependencies
- Kraken.js - Kraken is a light extension to express.js developed on a daily basis by PayPal. It helps us keep everything well organised and configurable per environment via json files. Kraken's actually a structure you end up with after weeks of creating project yourself using express.js (time saver), why to write in pure express then?
- Nunjucks - lightweight, well documented and supported template engine based on the Jinja syntax (same syntax as Twig for PHP)
- Grunt.js - JavaScript tasks runner, one of the two big competitors on the market (Grunt.js and Gulp). Grunt is more mature.