Created
June 25, 2013 15:24
-
-
Save muratcorlu/5859366 to your computer and use it in GitHub Desktop.
Connect middleware that creates mockes for rest apis.
This file contains hidden or 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
var fs = require('fs'); | |
module.exports = function (urlRoot, pathRoot) { | |
pathRoot = pathRoot.replace(urlRoot, ''); | |
return function(req, res, next){ | |
if (req.url.indexOf(urlRoot) === 0) { | |
// Ignore querystrings | |
url = req.url.split('?')[0]; | |
fs.readFile('./' + pathRoot + url + '/'+req.method+'.json', function(err, buf){ | |
if (err) return next(err); | |
resp = { | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': buf.length | |
}, | |
body: buf | |
}; | |
res.writeHead(200, resp.headers); | |
res.end(resp.body); | |
}); | |
} else { | |
next(); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment