Sindre Sorhus 11 years ago
commit
f9436f5e58
  1. 16
      .editorconfig
  2. 1
      .gitattributes
  3. 1
      .gitignore
  4. 19
      .jshintrc
  5. 3
      .travis.yml
  6. 44
      index.js
  7. 36
      package.json
  8. 29
      readme.md
  9. 27
      test.js

16
.editorconfig

@ -0,0 +1,16 @@
# editorconfig.org
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[package.json]
indent_style = space
indent_size = 2
[*.md]
trim_trailing_whitespace = false

1
.gitattributes

@ -0,0 +1 @@
* text eol=lf

1
.gitignore

@ -0,0 +1 @@
node_modules

19
.jshintrc

@ -0,0 +1,19 @@
{
"node": true,
"esnext": true,
"bitwise": true,
"camelcase": true,
"curly": true,
"eqeqeq": true,
"immed": true,
"indent": 4,
"newcap": true,
"noarg": true,
"quotmark": "single",
"regexp": true,
"undef": true,
"unused": true,
"strict": true,
"trailing": true,
"smarttabs": true
}

3
.travis.yml

@ -0,0 +1,3 @@
language: node_js
node_js:
- '0.10'

44
index.js

@ -0,0 +1,44 @@
'use strict';
var urlLib = require('url');
var http = require('http');
var https = require('https');
module.exports = function (url, cb) {
var redirectCount = 0;
var get = function (url, cb) {
var fn = urlLib.parse(url).protocol === 'https:' ? https : http;
fn.get(url, function (res) {
var ret = '';
// redirect
if (res.statusCode < 400 && res.statusCode >= 300 && res.headers.location) {
if (++redirectCount > 10) {
cb(new Error('Redirected 10 times. Aborting.'));
return;
}
get(urlLib.resolve(url, res.headers.location), cb);
return;
}
if (res.statusCode !== 200) {
cb(res.statusCode);
return;
}
res.setEncoding('utf8');
res.on('data', function (data) {
ret += data;
});
res.on('end', function () {
cb(null, ret);
});
}).on('error', cb);
};
get(url, cb);
};

36
package.json

@ -0,0 +1,36 @@
{
"name": "got",
"version": "0.0.0",
"description": "Simplified HTTP/HTTPS GET requests",
"license": "MIT",
"repository": "sindresorhus/got",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "http://sindresorhus.com"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha"
},
"files": [
"index.js"
],
"keywords": [
"http",
"https",
"get",
"got",
"url",
"uri",
"request",
"util",
"utility",
"simple"
],
"devDependencies": {
"mocha": "*"
}
}

29
readme.md

@ -0,0 +1,29 @@
# got [![Build Status](https://travis-ci.org/sindresorhus/got.svg?branch=master)](https://travis-ci.org/sindresorhus/got)
> Simplified HTTP/HTTPS GET requests
Follows redirects. Not intended to be feature-rich. Use [request](https://github.com/mikeal/request) if you need something more.
## Install
```bash
$ npm install --save got
```
## Usage
```js
var got = require('got');
got('http://todomvc.com', function (err, data) {
console.log(data);
//=> <!doctype html> ...
});
```
## License
[MIT](http://opensource.org/licenses/MIT) © [Sindre Sorhus](http://sindresorhus.com)

27
test.js

@ -0,0 +1,27 @@
'use strict';
var assert = require('assert');
var got = require('./index');
it('should request', function (done) {
got('http://google.com', function (err, data) {
if (err) {
console.error(err);
assert(false);
return;
}
assert(/google/.test(data));
done();
});
got('https://google.com', function (err, data) {
if (err) {
console.error(err);
assert(false);
return;
}
assert(/google/.test(data));
done();
});
});
Loading…
Cancel
Save