Skip to content

Instantly share code, notes, and snippets.

@liyu1981
Last active December 30, 2015 21:08
Show Gist options
  • Select an option

  • Save liyu1981/7885072 to your computer and use it in GitHub Desktop.

Select an option

Save liyu1981/7885072 to your computer and use it in GitHub Desktop.
Simple mock rest server of nodejs
var http = require('http');
var create = false;
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
if (req.url === '/rpc/Cloud/Host/List') {
res.end(JSON.stringify({ Items: [ { StateType: 5 } ] }));
} else if (req.url === '/rpc/Cloud/Injections/CreateRequestWith') {
if (create) {
res.end(JSON.stringify({ Err: 'Request again!' }));
process.exit(1);
} else {
res.end('OK');
create = true;
}
} else if (req.url === '/test') {
res.writeHeader(200, {'Access-Control-Allow-Origin': '*'});
var i = 0;
(function _do() {
res.write('progress: ' + (i*10) + '% done.\n');
if (i < 10) {
setTimeout(function() {
i += 1;
_do();
}, 500);
} else {
res.end('OK');
}
})();
}
}).listen(8765);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment