Permalink
Browse files

removed all plugins. removed plugin dependencies. updated controller …

…to work with new plugin versions
  • Loading branch information...
1 parent a878f2b commit fdf3b33b904746c4c36806cdb5fa8b8a49442ed3 @mikefrey mikefrey committed Oct 25, 2012
Showing with 32 additions and 481 deletions.
  1. +30 −6 controller.js
  2. +0 −2 index.js
  3. +2 −9 package.json
  4. +0 −35 plugins/contentNegotiator.js
  5. +0 −18 plugins/cookies.js
  6. +0 −114 plugins/errors.js
  7. +0 −11 plugins/index.js
  8. +0 −38 plugins/logging.js
  9. +0 −90 plugins/postData.js
  10. +0 −74 plugins/respond.js
  11. +0 −47 plugins/session.js
  12. +0 −26 plugins/templating.js
  13. +0 −11 plugins/timeout.js
View
36 controller.js
@@ -51,9 +51,10 @@ _.extend(Controller.prototype, EventEmitter.prototype, {
// Determines the best response type based on what the
// client can accept and what the controller can output.
- _render: function(mediaType) {
+ _render: function() {
var self = this
- var preferredType = mediaType || this.preferredMediaType(this.availableMediaTypes)
+
+ var preferredType = this.mediaType()
if (/html$/.test(preferredType)) {
return this.render(function(err, html){
@@ -73,12 +74,14 @@ _.extend(Controller.prototype, EventEmitter.prototype, {
throw 'No `xml` method implemented'
}
- // no match, try with the default media type
- if (!mediaType) return this._render(this._defaultMediaType)
-
- return this.res.end('No media type matched')
+ this.error(415, 'Media type not supported')
},
+ // Determines the media type to respond with. Override
+ // with your own content negotiation code.
+ mediaType: function() {
+ return this._defaultMediaType
+ },
// Initialize is an empty method by default. Override
// it with your own initialization logic.
@@ -102,6 +105,27 @@ _.extend(Controller.prototype, EventEmitter.prototype, {
layout: 'layout'
},
+ // A stub for rendering content from a template. Override
+ // with your own rendering code.
+ render: function() {
+ return ''
+ },
+
+ error: function() {
+ var args = Array.prototype.slice.call(arguments)
+ var status = 500
+ var message = 'An error occured'
+ args.forEach(function(v) {
+ if (typeof v === 'string')
+ message = v
+ else if (typeof v === 'number')
+ status = v
+ })
+ this.res.setHeader('content-type', 'text/plain')
+ this.res.statusCode = status
+ this.res.end(message)
+ },
+
// Call this in your initialize function to enforce
// a maximum request size. Useful for file uploads.
enforceMaxLength: function(maxLen) {
View
2 index.js
@@ -3,5 +3,3 @@ exports.Controller = require('./controller')
exports.Templating = require('./templating')
exports.Router = require('./router')
exports.Plugin = require('./plugin')
-
-exports.plugins = require('./plugins')
View
11 package.json
@@ -1,23 +1,16 @@
{
"name" : "nokomis",
- "version" : "0.0.2",
+ "version" : "0.1.0",
"description" : "A less opinionated web framework for node",
"main" : "./index.js",
"dependencies" : {
"async" : "*",
"bunyan": "*",
"cluster-master" : ">=0.0.10",
- "cookies": "~0.2.2",
- "error-page" : "*",
"extendable" : ">=0.0.3",
- "filed": "0",
"glob": "3",
- "keygrip": "~0.2.0",
- "negotiator": "~0.2.3",
- "request" : "*",
"routes" : "*",
- "underscore" : "*",
- "request-timeout" : "*"
+ "underscore" : "*"
},
"devDependencies" : {
"mocha" : "*",
View
35 plugins/contentNegotiator.js
@@ -1,35 +0,0 @@
-
-var Plugin = require('../plugin')
-var Negotiator = require('negotiator')
-
-var ContentNegotiator = module.exports = Plugin.extend({
-
- run: function(instance) {
- instance._neg = new Negotiator(instance.req)
- },
-
- preferredMediaType: function() {
- return this._neg.preferredMediaType.apply(this._neg, arguments)
- },
-
- preferredMediaTypes: function() {
- return this._neg.preferredMediaTypes.apply(this._neg, arguments)
- },
-
- preferredLanguage: function() {
- return this._neg.preferredLanguage.apply(this._neg, arguments)
- },
-
- preferredLanguages: function() {
- return this._neg.preferredLanguages.apply(this._neg, arguments)
- },
-
- preferredEncoding: function() {
- return this._neg.preferredEncoding.apply(this._neg, arguments)
- },
-
- preferredEncodings: function() {
- return this._neg.preferredEncodings.apply(this._neg, arguments)
- }
-
-})
View
18 plugins/cookies.js
@@ -1,18 +0,0 @@
-
-var Plugin = require('../plugin')
-var Cookies = require('cookies')
-var Keygrip = require('keygrip')
-
-module.exports = Plugin.extend({
-
- initialize: function(config) {
- this.cookieKeys = new Keygrip(config.secrets)
- },
-
- run: function(instance) {
- var req = instance.req
- var res = instance.res
- instance.cookies = req.cookies = new Cookies(req, res, this.cookieKeys)
- }
-
-})
View
114 plugins/errors.js
@@ -1,114 +0,0 @@
-
-var Plugin = require('../plugin')
-var Domain = require('domain')
-var ErrorPage = require('error-page')
-var _ = require('underscore')
-
-
-module.exports = Plugin.extend({
-
- initialize: function(config) {
- this.config = config
- },
-
- run: function(instance) {
- var req = instance.req
- var res = instance.res
- var config = this.config
-
- var defaultErrorPageConfig = {
- 404: handle404.bind(instance),
- 500: handle500.bind(instance),
- '*': handleError.bind(instance),
- debug: false
- }
-
- // setup error-page module
- var errorPageConfig = _.extend(defaultErrorPageConfig, config)
- instance.error = new ErrorPage(req, res, errorPageConfig)
-
- // Use a node Domain to handle all
- // unexpected errors in one location
-
- var domain = Domain.create()
- domain.add(req)
- domain.add(res)
-
- domain.on('error', function(err) {
- try {
- if (instance.error) {
- instance.error(err)
- } else {
- res.statusCode = 500
- res.setHeader('content-type', 'text/plain')
- res.end('Server Error\n' + err.message)
- }
-
- // don't destroy the domain before sending the error
- res.on('close', function() {
- domain.dispose()
- })
-
- // but don't wait forever
- setTimeout(function() {
- domain.dispose()
- }, 1000)
-
- // close down the server so a fresh worker can be started
- req.client.server.close()
-
- } catch (err) {
- domain.dispose()
- }
- })
- }
-
-})
-
-/**
- * Handle 404 error
- *
- * @param {Object} req
- * @param {Object} res
- * @param {Object} data
- * @api private
- */
-
-function handle404(req, res, data) {
- return handleError.call(this, req, res, data, 'errors/404')
-}
-
-/**
- * Handle 500 error
- *
- * @param {Object} req
- * @param {Object} res
- * @param {Object} data
- * @api private
- */
-
-function handle500(req, res, data) {
- return handleError.call(this, req, res, data, 'errors/500')
-}
-
-/**
- * Handle all response errors
- *
- * @param {Object} req
- * @param {Object} res
- * @param {Object} data
- * @param {String} template
- * @api private
- */
-
-function handleError(req, res, data, template) {
- console.error('Responding with error', data)
-
- delete data.options
- delete data.stack
- delete data.error
-
- this.template = template || 'errors/default'
- this.model = data
- return this._render(template, data)
-}
View
11 plugins/index.js
@@ -1,11 +0,0 @@
-
-var glob = require('glob')
-
-var plugins = module.exports = {}
-
-glob.sync(__dirname + '/!(index.js)').forEach(function(file) {
- var module = require(file)
- var name = file.substring(file.lastIndexOf('/')+1, file.lastIndexOf('.'))
- name = name.charAt(0).toUpperCase() + name.slice(1)
- plugins[name] = module
-})
View
38 plugins/logging.js
@@ -1,38 +0,0 @@
-
-var Plugin = require('../plugin')
-var crypto = require('crypto')
-var bunyan = require('bunyan')
-
-module.exports = Plugin.extend({
-
- initialize: function(config) {
- this._logger = bunyan.createLogger(config || { name:'Nokomis App' })
- },
-
- run: function(instance) {
- var req = instance.req
- var res = instance.res
- instance.log = res.log = req.log = this._logger.child({
- serializers: bunyan.stdSerializers,
- req_id: crypto.randomBytes(4).toString('hex'),
- session: req.sessionToken
- })
-
- // log information about the request and
- // the client making it
- var remoteAddr = req.socket.remoteAddress + ':' + req.socket.remotePort
- var address = req.socket.address()
- address = address.address + ':' + address.port
- req.log.info({
- req: req,
- remote: remoteAddr,
- address: address
- })
-
- // log the response when the request has finished
- req.on('finish', function() {
- req.log.info({ res: res })
- })
- }
-
-})
View
90 plugins/postData.js
@@ -1,90 +0,0 @@
-// Handles data sent via POST or PUT
-
-// TODO: Gut this and use https://github.com/felixge/node-formidable
-// However, node-formidable does not have json upload support so
-// it will need to be patched.
-
-var Plugin = require('../plugin')
-var qs = require('querystring')
-var StringDecoder = require('string_decoder').StringDecoder
-
-module.exports = Plugin.extend({
-
- initialize: function(config) {
-
- },
-
- run: function(instance) {
- //instance.cookies = new Cookies(instance.req, instance.res, config.cookiesKeys)
- var req = instance.req
-
- var body = ''
- var data = new StringDecoder
- req.on('data', function(c) {
- body += data.write(c)
- })
- req.on('end', function() {
- req.body = body
- req.emit('body', body)
- })
-
- req.data = {
- json: function(callback) {
- if (!req.headers['content-type'].match(/\/(x-)?json$/)) {
- callback({statusCode:415})
- }
- if (req.body)
- return processJSON(req.body, callback)
- req.on('body', function(body) {
- processJSON(body, callback)
- })
- },
- form: function(callback) {
- if (req.headers['content-type'] !== 'application/x-www-form-urlencoded') {
- return res.error(415)
- }
- if (req.body) return processForm(req.body, callback)
- req.on('body', function(body) {
- processForm(body, callback)
- })
- },
- body: function(callback) {
- if (req.body) return callback(req.body)
- req.on('body', function(body) {
- callback(null, body)
- })
- }
- }
- }
-
-})
-
-/**
- * Attempt to process the body data into JSON
- *
- * @param {String} body
- * @param {Function} callback
- */
-
-function processJSON(body, callback) {
- var json
- try {
- json = JSON.parse(body)
- } catch (err) {
- err.statusCode = 400
- return callback(err)
- }
- return callback(null, json)
-}
-
-/**
- * Attempt to process the body
- * data into an object hash
- *
- * @param {String} body
- * @param {Function} callback
- */
-
-function processForm(body, callback) {
- callback(null, qs.parse(body))
-}
View
74 plugins/respond.js
@@ -1,74 +0,0 @@
-
-var Plugin = require('../plugin')
-
-var Respond = module.exports = Plugin.extend({
-
- /**
- * Provides a redirect helper on the res object
- *
- * @param {String} target
- * @param {Number} code
- * @api public
- */
-
- redirect: function (target, code) {
- this.res.statusCode = code || 302
- this.res.setHeader('location', target)
- var avail = ['text/html', 'application/json']
- var mt = req.contentNegotiator.preferredMediaType(avail)
- if (mt === 'application/json') {
- this.json({ redirect: target, statusCode: code })
- } else {
- this.html( '<html><body><h1>Moved'
- + (code === 302 ? ' Permanently' : '') + '</h1>'
- + '<a href="' + target + '">' + target + '</a>')
- }
- },
-
- /**
- * Provides a send helper
- *
- * @param {String} data
- * @param {Number} status
- * @param {Object} headers
- * @api public
- */
-
- send: function (data, status, headers) {
- var res = this.res
- res.statusCode = res.statusCode || status
- if (headers) Object.keys(headers).forEach(function (h) {
- res.setHeader(h, headers[h])
- })
- if (!Buffer.isBuffer(data)) data = new Buffer(data)
- res.setHeader('content-length', data.length)
- res.end(data)
- },
-
- /**
- * JSON helper for returning a json object
- * in the response
- *
- * @param {Object} obj
- * @param {Number} status
- * @api public
- */
-
- json: function (obj, status) {
- this.send(JSON.stringify(obj), status, {'content-type':'application/json'})
- },
-
- /**
- * HTML helper for returning an html string
- * in the response
- *
- * @param {String} data
- * @param {Number} status
- * @api public
- */
-
- html: function (data, status) {
- this.send(data, status, {'content-type':'text/html'})
- }
-
-})
View
47 plugins/session.js
@@ -1,47 +0,0 @@
-
-var Plugin = require('../plugin')
-
-module.exports = Plugin.extend({
-
- run: function(instance) {
- var sessConf = instance.config.session || { key:'s' }
- instance.session = new Session(instance.req, sessConf)
- }
-
-})
-
-
-// Everything from here on down should be moved into it's own module
-var options = {
- signed: true
-}
-
-function Session(req, config) {
- this.cookiekey = config.key
- this.cookies = req.cookies
- this.data = this.cookies.get(this.cookiekey) || {}
-}
-
-Session.prototype.get = function(key, cb) {
- if (typeof key == 'function') return key(this.data)
- return cb(this.data[key])
-}
-
-Session.prototype.set = function(key, value, cb) {
- var obj = key
- if (typeof key == 'string') {
- obj = {}
- obj[key] = value
- }
-
- Object.keys(obj).forEach(function(key) {
- this.data[key] = obj[key]
- })
-
- this.cookies.set(this.cookiekey, this.data)
-}
-
-Session.prototype.del = function() {
- delete this.data[del]
- this.cookies.set(this.cookiekey, this.data)
-}
View
26 plugins/templating.js
@@ -1,26 +0,0 @@
-var Plugin = require('../plugin')
-var _ = require('underscore')
-var path = require('path')
-var extendable = require('extendable')
-
-var Templating = require('../templating')
-
-module.exports = Plugin.extend({
-
- initialize: function(config) {
- var tmpl = this.tmpl = new Templating()
- tmpl.templatePath = config.templatePath || tmpl.templatePath
- },
-
- run: function(instance) {
- instance.tmpl = this.tmpl
- },
-
- render: function(callback) {
- var tmpl = this.template
- var data = this.model
- var options = this.templateOptions || {}
- this.tmpl.render(tmpl, data, options, callback)
- }
-
-})
View
11 plugins/timeout.js
@@ -1,11 +0,0 @@
-var Plugin = require('../plugin')
-var requestTimeout = require('request-timeout')
-
-module.exports = Plugin.extend({
-
- run: function(instance) {
- var seconds = instance.config.requestTimeout
- requestTimeout(instance.req, instance.res, seconds)
- }
-
-})

0 comments on commit fdf3b33

Please sign in to comment.