Package Exports
- node-fetch
- node-fetch/lib/response
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-fetch) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
node-fetch
A light-weight module that brings window.fetch to node.js
Motivation
I really like the notion of Matt Andrews' isomorphic-fetch: it bridges the API gap between client-side and server-side http requests, so developers have less to worry about.
But I think the term isomorphic is generally misleading: it gives developers a false sense of security that their javascript code will run happily on both controlled server environment as well as uncontrollable user browsers. When the latter is only true for a subset of modern browsers, not to mention quirks in native implementation.
Instead of implementing XMLHttpRequest in node to run browser-specific fetch polyfill, why not go from node's http to fetch API directly? Node has native stream support, browserify build targets (browsers) don't, so underneath they are going to be vastly different anyway.
Hence node-fetch, minimal code for a window.fetch compatible API on node.js runtime.
Features
- Stay consistent with
window.fetchAPI. - Make conscious trade-off when following whatwg fetch spec and stream spec implementation details, document known difference.
- Use native promise, but allow substituting it with [insert your favorite promise library].
Difference from client-side fetch
- See Known limits for details.
- If you happen to use a missing feature that
window.fetchoffers, feel free to open an issue. - Pull requests are welcomed too!
Install
npm install node-fetch --save
Usage
var fetch = require('node-fetch');
// 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);
});
// meta
fetch('https://github.com/')
.then(function(res) {
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
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.11+, yield with co
var co = require('co');
co(function *() {
var res = yield fetch('https://api.github.com/users/github')
.then(function(res) {
return res.json();
}).then(function(json) {
console.log(json);
});
console.log(res);
});See test cases for more examples.
License
MIT
Acknowledgement
Thanks to github/fetch for providing a solid implementation reference.