ninjs-request
Version:
request engine
181 lines (149 loc) • 4.63 kB
JavaScript
'use strict'
const _ = require('ninjs-lodash')
const request = require('../')
const fs = require('fs-extra')
const http = require('http')
const _NAME = _.path.name(__filename)
const _NS = `ninjs.request.test.${_NAME}`
const _URL = 'https://google.com'
module.exports = {
NAME: _NAME,
NS: _NS,
DIR: __dirname,
FILE: __filename,
run: run,
get: get
}
function run(options) {
toFile()
toPut()
toPost()
getPut()
getPost()
getPutWithResponseEvent()
getPutWithErrorEvent()
httpTest()
advancedHttpTest()
piping()
}
// You can stream any response to a file stream.
function toFile() {
request('http://google.com/doodle.png')
.pipe(fs.createWriteStream('doodle.png'))
}
// You can also stream a file to a PUT or POST request.
// This method will also check the file extension against
// a mapping of file extensions to content-types
// (in this case application/json) and use the proper content-type
// in the PUT request (if the headers don’t already provide one).
function toPut() {
fs
.createReadStream('file.json')
.pipe(request.put('http://mysite.com/obj.json'))
}
function toPost() {
fs
.createReadStream('file.json')
.pipe(request.post('http://mysite.com/obj.json'))
}
// Request can also pipe to itself.
// When doing so, content-type and content-length are preserved in the PUT headers.
function getPut() {
request
.get('http://google.com/img.png')
.pipe(request.put('http://mysite.com/img.png'))
}
function getPost() {
request
.get('http://google.com/img.png')
.pipe(request.post('http://mysite.com/img.png'))
}
// Request emits a "response" event when a response is received.
// The response argument will be an instance of http.IncomingMessage.
function getPutWithResponseEvent() {
request
.get('http://google.com/img.png')
.on('response', function(response) {
console.log(response.statusCode) // 200
console.log(response.headers['content-type']) // 'image/png'
})
.pipe(request.put('http://mysite.com/img.png'))
}
// To easily handle errors when streaming requests,
// listen to the error event before piping:
function getPutWithErrorEvent() {
request
.get('http://mysite.com/doodle.png')
.on('error', function(err) {
console.log(err)
})
.pipe(fs.createWriteStream('doodle.png'))
}
// Now let’s get fancy.
function httpTest() {
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
if (req.method === 'PUT') {
req.pipe(request.put('http://mysite.com/doodle.png'))
} else if (req.method === 'GET' || req.method === 'HEAD') {
request.get('http://mysite.com/doodle.png').pipe(resp)
}
}
})
}
// You can also pipe() from http.ServerRequest instances,
// as well as to http.ServerResponse instances.
// The HTTP method, headers, and entity-body data will be sent.
// Which means that, if you don't really care about security, you can do:
function advancedHttpTest() {
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
var x = request('http://mysite.com/doodle.png')
req.pipe(x)
x.pipe(resp)
}
})
}
// And since pipe() returns the destination
// stream in ≥ Node 0.5.x you can do one line proxying. :)
function piping() {
req.pipe(request('http://mysite.com/doodle.png')).pipe(resp)
}
// Also, none of this new functionality
// conflicts with requests previous features, it just expands them.
function proxy() {
var r = request.defaults({ 'proxy':'http://localproxy.com' })
http.createServer(function (req, resp) {
if (req.url === '/doodle.png') {
r.get('http://google.com/doodle.png').pipe(resp)
}
})
}
Use Request as a Writable stream to easily `POST` Readable streams (like files, other HTTP requests, or otherwise).
TL;DR: Pipe a Readable Stream onto Request via:
```
READABLE.pipe(request.post(URL));
```
A more detailed example:
```js
var fs = require('fs')
, path = require('path')
, http = require('http')
, request = require('request')
, TMP_FILE_PATH = path.join(path.sep, 'tmp', 'foo')
;
// write a temporary file:
fs.writeFileSync(TMP_FILE_PATH, 'foo bar baz quk\n');
http.createServer(function(req, res) {
console.log('the server is receiving data!\n');
req
.on('end', res.end.bind(res))
.pipe(process.stdout)
;
}).listen(3000).unref();
fs.createReadStream(TMP_FILE_PATH)
.pipe(request.post('http://127.0.0.1:3000'))
;
```