InterCity.js - express.js-based, modular and lightweight NodeJS framework/structure
Main components:
- Express.js (HTTP Server)
- Nunjucks (View Engine)
Development Environment:
npm install
Production Environment:
npm install --production
Setting Port to listen on (default=8080):
PORT=8080 node app.js
Enabling Server logs (disabled by default):
DEBUG=IC node app.js
Setting Environment (default=development):
NODE_ENV=<production|development> node app.js
You can mix options, examples:
NODE_ENV=production PORT=80 DEBUG=IC node app.js
You can preview your app under http://localhost:8080 address unless you specified different port.
The app doesn't reload itself automatically. To avoid overhead with restarting the app manually every time you change your code, you can use nodemon tool. Simply run following commands:
sudo npm install -g nodemon
In your app directory:
DEBUG=IC nodemon app.js
Since Nunjucks support builtin filters as well as custom filters, you can roll out your own filters easily by creating JavaScript file in the views/filters
directory.
// Location: config/filters/nl2br.js
module.exports = function (nunjucks) {
/**
* Replaces all occurences of newline with <br> tags
*
* Sample Usage:
*
* {{ 'Hello\n\nWorld' | nl2br }} -> Hello<br><br>World
*
*/
return function (string) {
return new nunjucks.runtime.SafeString(string.replace(/\n/g, '<br>'));
};
};
}
The code above would create nl2br
filter that will be globally available in all your nunjucks templates. Filter is nothing else than the name of the file without extension, e.g. nl2br.js -> nl2br
.
Feel free to contribute or contact me at contact@maciejsmolinski.com with any questions