Skip to content

Instantly share code, notes, and snippets.

@chriszs
Last active August 29, 2015 14:13
Show Gist options
  • Save chriszs/24d5ba42becdfe033bcf to your computer and use it in GitHub Desktop.
Save chriszs/24d5ba42becdfe033bcf to your computer and use it in GitHub Desktop.
Here's how you can use express.js' handle() to call routes directly. YMMV.
var express = require('express');
var app = express();
app.get('/your/route/here',function(req, res){
res.send('hello world');
});
function request(method,route,cb) {
app.handle({
method: method,
url: route
},{
finish: null,
on: function (type,fn) {
if (type == 'finish') {
this.finish = fn;
}
},
setHeader: function () {
},
end: function (buffer,encoding) {
cb(null,buffer.toString(encoding));
this.finish();
}
},function (err) {
cb(err);
});
}
request('get','/your/route/here',function (err,result) {
if (err) throw err;
console.log('got: ' + result);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment