Skip to content

Instantly share code, notes, and snippets.

@lusentis
Last active December 26, 2015 21:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lusentis/7216186 to your computer and use it in GitHub Desktop.
Save lusentis/7216186 to your computer and use it in GitHub Desktop.
Simple express-like methods for connect
node_modules
npm-debug.log
/*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();
};
};
{
"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