skellyjs

skellyjs framework

Usage no npm install needed!

<script type="module">
  import skellyjs from 'https://cdn.skypack.dev/skellyjs';
</script>

README

skellyjs

skellyjs framework

skellyjs

npm version travis test Coverage Status

Table of Contents

  1. Quick Start
  2. Creating Pages (Controllers)
  3. Models
  4. HTML Views
  5. Client-side Javascript Files
  6. CSS Files
  7. Logging
  8. Custom Installation
  9. Node.js Style Guide
  10. License

Quick Start

Install the Generator

$ npm install -g generator-skellyjs

Create the app:

$ skelly /tmp/project && cd /tmp/project

Install dependencies:

$ npm install

Start the app

$ npm start

⬆ back to top

Creating Pages (Controllers)

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.

⬆ back to top

Models

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);
      }
    }
});

⬆ back to top

HTML Views

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.

⬆ back to top

Client-side Javascript Files

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

⬆ back to top

CSS Files

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';

⬆ back to top

Logging

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.

⬆ back to top

Custom Installation

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);

⬆ back to top

Node.js Style Guide

NODESTYLEGUIDE

⬆ back to top

License

MIT

⬆ back to top