needle 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env node
  2. var needle = require('./../lib/needle');
  3. function exit(code, str) {
  4. console.log(str) || process.exit(code);
  5. }
  6. function usage() {
  7. var out = ['Usage: needle [get|head|post|put|delete] url [query]'];
  8. out.push('Examples: \n needle get google.com\n needle post server.com/api foo=bar');
  9. exit(1, out.join('\n'))
  10. }
  11. if (process.argv[2] == '-v' || process.argv[2] == '--version')
  12. exit(0, needle.version);
  13. else if (process.argv[2] == null)
  14. usage();
  15. var method = process.argv[2],
  16. url = process.argv[3],
  17. options = { compressed: true, parse_response: true, follow_max: 5, timeout: 10000 };
  18. if (!needle[method]) {
  19. url = method;
  20. method = 'get';
  21. }
  22. var callback = function(err, resp) {
  23. if (err) return exit(1, "Error: " + err.message);
  24. if (process.argv.indexOf('-i') != -1)
  25. console.log(resp.headers) || console.log('');
  26. console.log(resp.body.toString());
  27. };
  28. if (method == 'post' || method == 'put')
  29. needle[method](url, process.argv[4], options, callback);
  30. else
  31. needle[method](url, options, callback);