Igloo
Igloo is a library of components built with electrolyte to be used as building blocks for an app.
Need to view documentation for a given component? See Components below.
Index
Sponsor
Install
Please use eskimo which has igloo built-in.
However you can use igloo in your own project by installing it.
npm install -s igloo
Usage
You will need to first require('igloo')
, define a "config" component, and then use electrolyte
's IoC.loader
method to load igloo
's components ([listed below][#components]).
Note that
igloo
requires you to have defined a "config" component (sinceigloo
's "setting" component requires it as a dependency). For example, Eskimo defines a "config" component here.
// boot/config.js
exports = module.exports = function() {
return {
defaults: {
logger: {
console: true,
requests: true,
mongo: false,
file: false,
hipchat: false
},
output: {
handleExceptions: true,
colorize: true,
prettyPrint: false
}
},
development: {
port: 3000,
},
production: {
port: 3080,
requests: false,
output: {
colorize: false,
prettyPrint: false
}
}
}
}
exports['@singleton'] = true
// app.js
var IoC = require('electrolyte')
var igloo = require('igloo')
var path = require('path')
var express = require('express')
var winstonRequestLogger = require('winston-request-logger')
IoC.loader(IoC.node(path.join(__dirname, 'boot')))
IoC.loader('igloo', igloo)
// logger component
var logger = IoC.create('igloo/logger')
logger.info('Hello world')
// settings component
var settings = IoC.create('igloo/settings')
// basic server
var app = express()
// winston request logger before everything else
// but only if it was enabled in settings
if (settings.logger.requests)
app.use(winstonRequestLogger.create(logger))
app.get('/', function(req, res, next) {
res.send("It's cold outside, but warm in the igloo")
})
app.listen(settings.port, function() {
logger.info('Server started on port %d', settings.port)
})
Components
error-handler
Returns Express route middleware for error handling.
// example - error handler
var _ = require('underscore')
var errorHandler = IoC.create('igloo/error-handler')
app.post('/user', createUser, errorHandler)
function createUser(req, res, next) {
if (!_.isString(req.body.name))
return next({
param: 'name',
message: 'User name is missing'
})
// ... create a user in your db
res.send('You successfully created a user')
}
knex
Returns a Knex SQL database connection using
settings.knex
.
// example - knex
var knex = IoC.create('igloo/knex')
app.get('/users', function(req, res, next) {
knex
.raw('SELECT * FROM USERS LIMIT 10')
.exec(function(err, results) {
if (err) return next(err)
res.send(results)
})
})
logger
Returns a Winston logger object with pre-configured transports using
settings.logger
andsettings.output
.
// example - logger
var logger = IoC.create('igloo/logger')
logger.info('Hello world')
mongo
Returns a MongoDB NoSQL connection using
settings.logger
,settings.output
, andsettings.mongo
and Mongoose ORM.TODO: Should we rename this to
igloo/mongoose
?
// example - mongo
var validator = require('validator')
var mongoose = IoC.create('igloo/mongo')
var User = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
validate: validator.isEmail
}
})
mongoose.model('User', User)
mongoose-plugin
Returns a Mongoose plugin helper for common schema additions.
// example - mongoose plugin
var validator = require('validator')
var mongoose = IoC.create('igloo/mongo')
var mongoosePlugin = IoC.create('igloo/mongoose-plugin')
var User = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true,
validate: validator.isEmail
}
})
User.plugin(mongoosePlugin)
mongoose.model('User', User)
server
Returns a function which accepts a callback argument (currently no
err
argument is returned when this callback function is executed). This component starts either a cluster (with one thread per CPU) or a simple single-threaded instance usingapp.listen
. This should be used withapp.phase
using bootable, since bootable's "phases" (app.phase
) method accepts this callback function as a standard argument when defining a "phase" (read more about bootable to understand this please). This component usessettings.logger
,settings.output
, andsettings.server
.
// example - server
var bootable = require('bootable')
var express = require('express')
var app = bootable(express())
var server = IoC.create('igloo/server')
var logger = IoC.create('igloo/logger')
// this should always be the last phase
app.phase(server)
app.boot(function(err) {
if (err) {
logger.error(err.message)
if (settings.showStack)
logger.error(err.stack)
process.exit(-1)
return
}
logger.info('app booted')
})
sessions
Returns a Redis connection using
settings.logger
,settings.output
, andsettings.redis
with express-session and connect-redis.
// example - sessions
var session = require('express-session')
var express = require('express')
var app = express()
var sessions = IoC.create('igloo/sessions')
var settings = IoC.create('igloo/settings')
// add req.session cookie support
settings.session.store = sessions
app.use(session(settings.session))
settings
Returns a settings object using a "config" component defined and loaded by electrolyte in a location such as "boot/config" (see initial example at the top of this Readme).
For a full config example file, see Eskimo's default config.
TODO: Check into requiring a "local" component so Adnan's work is tested
update-notifier
Returns nothing, as it is used for notifying an app of module updates using update-notifier.
TODO: Look into why this isn't working per https://github.com/yeoman/update-notifier/issues/25
Tests
npm install -d
npm test
Conventions
See nifty-conventions for code guidelines, general project requirements, and git workflow.
Contributors
- Nick Baugh niftylettuce@gmail.com
- Adnan Ibrišimbegović adibih@gmail.com
Credits
- Igloo by VALÈRE DAYAN from The Noun Project
- ESKIMO IGLOO (color palette)