Last active
December 26, 2015 21:29
-
-
Save lusentis/7216186 to your computer and use it in GitHub Desktop.
Simple express-like methods for connect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules | |
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*jshint node:true, indent:2, unused:true, undef:true, laxcomma:true */ | |
/* ~ Express-like methods & shortcuts ~ */ | |
var jade = require('jade') | |
, swallow = require('node-swallow') | |
, path = require('path') | |
; | |
exports.expressSucks = function (opts) { | |
opts = opts || {}; | |
var tpl_basedir = opts.basedir || path.join(process.cwd(), 'views'); | |
return function _shim(req, res, next) { | |
// Request | |
req.ip = req.connection.remoteAddress; | |
req.param = function (key) { | |
if (req.params && req.params.hasOwnProperty(key)) { | |
return req.params[key]; | |
} else if (req.query && req.query.hasOwnProperty(key)) { | |
return req.query[key]; | |
} else if (req.body && req.body.hasOwnProperty(key)) { | |
return req.body[key]; | |
} else { | |
return undefined; | |
} | |
}; | |
// Response | |
res.send = function (str) { | |
res.write(str); | |
}; | |
res.status = function (status) { | |
res.statusCode = status; | |
return res; | |
}; | |
res.json = function (obj) { | |
res.setHeader('Content-type', 'application/json'); | |
res.end(JSON.stringify(obj)); | |
}; | |
res.render = function (templateFile, locals) { | |
jade.renderFile(path.join(tpl_basedir, templateFile + '.jade'), locals, swallow('while rendering a template', function (html) { | |
res.setHeader('Content-type', 'text/html'); | |
res.end(html); | |
})); | |
}; | |
res.redirect = function (where) { | |
res.statusCode = 302; | |
res.setHeader('Location', where); | |
res.end('Moved to ' + where); | |
}; | |
next(); | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "expresssucks", | |
"version": "0.2.2", | |
"description": "Simple express-like methods for connect", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "git@gist.github.com:7216186.git" | |
}, | |
"keywords": [ | |
"express", | |
"connect" | |
], | |
"author": "Simone Lusenti <simone@plasticpanda.com>", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/7216186" | |
}, | |
"dependencies": { | |
"node-swallow": "0.0.3", | |
"jade": "~0.35.0" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment