public
Last active

Simple express-like methods for connect

  • Download Gist
.gitignore
1 2
node_modules
npm-debug.log
index.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
/*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();
};
};
package.json
JSON
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
{
"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"
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.