Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Created June 11, 2017 14:26
Show Gist options
  • Save xgqfrms-GitHub/055b547f7a121162151517c58647dab2 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/055b547f7a121162151517c58647dab2 to your computer and use it in GitHub Desktop.
Travis CI & 测试 .travis.yml
@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Jun 11, 2017

@xgqfrms-GitHub
Copy link
Author

language: node_js

language: node_js
dist: dist
node:
- 6.10.3
npm:
- 5.0.2
os:
- windows10 x64 pro
script: npm run nct

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

G:\wwwRoot\learning\JavaScript\Ajax\js\node-cli-tools\node_modules\node-fetch

language: node_js
node_js:
  - "0.10"
  - "0.12"
  - "node"
env:
  - FORMDATA_VERSION=1.0.0
  - FORMDATA_VERSION=2.1.0
before_script:
  - 'if [ "$FORMDATA_VERSION" ]; then npm install form-data@^$FORMDATA_VERSION; fi'
before_install: if [[ `npm -v` < 3 ]]; then npm install -g npm@1.4.28; fi
script: npm run coverage

@xgqfrms-GitHub
Copy link
Author

https://www.npmjs.com/package/node-fetch

var fetch = require('node-fetch');
 
// if you are on node v0.10, set a Promise library first, eg. 
// fetch.Promise = require('bluebird'); 
 
// plain text or html 
 
fetch('https://github.com/')
    .then(function(res) {
        return res.text();
    }).then(function(body) {
        console.log(body);
    });
 
// json 
 
fetch('https://api.github.com/users/github')
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });
 
// catching network error 
// 3xx-5xx responses are NOT network errors, and should be handled in then() 
// you only need one catch() at the end of your promise chain 
 
fetch('http://domain.invalid/')
    .catch(function(err) {
        console.log(err);
    });
 
// stream 
// the node.js way is to use stream when possible 
 
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
    .then(function(res) {
        var dest = fs.createWriteStream('./octocat.png');
        res.body.pipe(dest);
    });
 
// buffer 
// if you prefer to cache binary data in full, use buffer() 
// note that buffer() is a node-fetch only API 
 
var fileType = require('file-type');
fetch('https://assets-cdn.github.com/images/modules/logos_page/Octocat.png')
    .then(function(res) {
        return res.buffer();
    }).then(function(buffer) {
        fileType(buffer);
    });
 
// meta 
 
fetch('https://github.com/')
    .then(function(res) {
        console.log(res.ok);
        console.log(res.status);
        console.log(res.statusText);
        console.log(res.headers.raw());
        console.log(res.headers.get('content-type'));
    });
 
// post 
 
fetch('http://httpbin.org/post', { method: 'POST', body: 'a=1' })
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });
 
// post with stream from resumer 
 
var resumer = require('resumer');
var stream = resumer().queue('a=1').end();
fetch('http://httpbin.org/post', { method: 'POST', body: stream })
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });
 
// post with form-data (detect multipart) 
 
var FormData = require('form-data');
var form = new FormData();
form.append('a', 1);
fetch('http://httpbin.org/post', { method: 'POST', body: form })
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });
 
// post with form-data (custom headers) 
// note that getHeaders() is non-standard API 
 
var FormData = require('form-data');
var form = new FormData();
form.append('a', 1);
fetch('http://httpbin.org/post', { method: 'POST', body: form, headers: form.getHeaders() })
    .then(function(res) {
        return res.json();
    }).then(function(json) {
        console.log(json);
    });
 
// node 0.12+, yield with co 
 
var co = require('co');
co(function *() {
    var res = yield fetch('https://api.github.com/users/github');
    var json = yield res.json();
    console.log(res);
});

@xgqfrms-GitHub
Copy link
Author

let username = `xgqfrms-GitHub`;
    repo = `Node-CLI-Tools/commits`;

fetch(`https://api.github.com/users/${username}/${repo}`,{
    data: {
        client_id: '08ecc2f68d922f18800e',
        client_secret: '5846d428b5340812b76c9637eceaee979340b922'
    }
})
.then((response) => response.json())
.then((json)=> {
    console.log(`json = ${json}`);
    return repos = json;
})
.then((repos)=>{
    console.log(`repos = ${repos}`);
    console.log(`repos = ${repos.length}`);
    console.log(`repos$ 0  = ${repos[0]}`);
    console.log(`repos$ 1  = ${repos[1]}`);
    for (let i = 0; i < repos.length; i++) {
        console.log(`repos${i}  = ${repos[i]}`);
    }
});




let username = `xgqfrms-GitHub`;

fetch(`https://api.github.com/users/${username}/repos`,{
    data: {
        client_id: '08ecc2f68d922f18800e',
        client_secret: '5846d428b5340812b76c9637eceaee979340b922'
    }
})
.then((response) => response.json())
.then((json)=> {
    console.log(`json = ${json}`);
    return repos = json;
})
.then((repos)=>{
    console.log(`repos = ${repos}`);
    console.log(`repos = ${repos.length}`);
    console.log(`repos$ 0  = ${repos[0]}`);
    console.log(`repos$ 1  = ${repos[1]}`);
    for (let i = 0; i < repos.length; i++) {
        console.log(`repos${i}  = ${repos[i]}`);
    }
});

@xgqfrms-GitHub
Copy link
Author

nct-test-ok

@xgqfrms-GitHub
Copy link
Author

nct-react-test-ok

@xgqfrms-GitHub
Copy link
Author

demo

$ nct

$ nct xgqfrms react

nct

nct-test-ok

nct xgqfrms react

nct-react-test-ok

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment