skellyjs framework
- Quick Start
- Creating Pages (Controllers)
- Models
- HTML Views
- Client-side Javascript Files
- CSS Files
- Logging
- Custom Installation
- Node.js Style Guide
- License
Install the Generator
$ npm install -g generator-skellyjs
$ skelly /tmp/project && cd /tmp/project
$ npm install
$ npm start
The built in router will automatically look for a controller named the same as the url path. The index controller is used for /. To create a /help page, just create a controller named help.js. Index and 404 controller examples are in /controllers.
To connect to your MongoDB, make sure to set process.env.DB_HOST
, process.env.DB_NAME
, process.env.DB_USER
, and process.env.DB_PASSWORD
. DotEnv is built into skelly, so if you create a .env file in your application's root, you should set these values there. A .env example file is in the root.
/* /.env */
# Database host(s) comma separated (10.0.0.1,10.0.0.2)
DB_HOST=localhost
# Database Name
DB_NAME=skelly
# Database user
DB_USER=user
# Database password
DB_PASSWORD=pass
Mongoose models will be included automatically in the skelly object. To access them, use the skelly.models.<filename>
method. An index model example is in /models.
The model itself is constructed just like all other Mongoose models, but using the skelly.mongoose object (instead of including the mongoose library)
/* /models/index.js */
module.exports = function(skelly) {
return skelly.mongoose.model(
// name of your model (http://mongoosejs.com/docs/models.html)
'Test',
// schema (http://mongoosejs.com/docs/schematypes.html)
{
title : {
type: String,
required: true
}
}
);
};
In your controller, access the index model using skelly.models.index
.
/* /controllers/index.js */
skelly.models.index.findOne({}, function(err, index) {
if (err) {
skelly.log.error(err);
res.end(err);
} else {
// if no entry, just pass a static title
if (!index) {
skelly.render(req, res, viewFile, {title:"Hello, my name is Shelby!"});
// if there's an entry, pass it to use as the title
} else {
skelly.render(req, res, viewFile, index);
}
}
});
The built in templating engine is swig. Your views should go into the /views folder. Javascript (/javascripts )and CSS (/stylesheets) includes will be read into memory. You can hash javascript, css, or images using a skelly swig filter. Index and 404 views, plus a main layout, examples are in /views (and /views/layouts).
<script src="/javascripts/{{'index.min.js'|hash}}"></script>
<link rel="stylesheet" href="/stylesheets/{{'index.min.css'|hash}}" />
<img src="/images/{{'shelby.jpg'|hash}}" />
Example output:
<script src="/javascripts/index.min.e0df532694.js"></script>
<link rel="stylesheet" href="/stylesheets/index.min.e0df532694.css" />
<img src="/images/shelby.e0df532694.jpg'" />
The system will automatically return the current file for any hash.
The javascript files are read into memory on load. Required files are not combined into a single file, but that feature is coming. They are, however, minified using Uglify-JS. An index javascript example file is located in /javascripts
The built in CSS precompiler is LESS. I suggest you create a single less file for each view (/stylesheets), and include global less files (/stylesheets/includes) as needed. An index less file, and several includes, are located in /stylesheets (and /styplesheets/includes)
/* index.less */
@import 'global';
To log something to stdout, there's a built in method (using bunyan). You can simply call skelly.log.<level>('Hello!')
.
level
can be (from most severe to least):
- fatal
- error
- warn
- info
- debug
- trace
By default (development mode), debug and higher are output, while trace is ignored. In production (NODE_ENV=production
), info and higher are output, while debug and trace are ignored. You can set the log level LOGLEVEL=trace
in your .env file.
If you'd like to install the framework into your own app:
$ npm install skellyjs --save
In your main script:
/* /app.js */
var http = require('http'); // http server
var skelly = require('skellyjs'); // skellyjs framework
// generate the css
skelly.generateCss();
// generate the javascript
skelly.generateJs();
// create the server
var server = http.createServer(function(req, res) {
skelly.router(req,res);
});
// accept incoming traffic
server.listen(process.env.PORT || 4000);
skelly.log.debug('Listening on port:', server.address().port);
skelly.log.debug("Hash:",skelly.hash);