diff --git a/examples/simple-http-forwarding_noCors.js b/examples/simple-http-forwarding_noCors.js new file mode 100644 index 0000000..280f780 --- /dev/null +++ b/examples/simple-http-forwarding_noCors.js @@ -0,0 +1,62 @@ +/** + * (c) 2017 Radio France + * This program is free software: you can redistribute it and/or modify it under the terms of the CeCILL-B license + */ + +/* + * Example : Simple forwarding + */ + +/* eslint-disable no-console */ +const http = require('http') +const fs = require('fs') +const Forwarder = require('../lib/Forwarder') + +const server = new Forwarder({ + // The servers to forward the request to + targets: ['http://127.0.0.1:9001', 'http://127.0.0.1:9002'], + // Add a header to the request before forwarding + targetHeaders: { 'some-header': 'some-val' }, + // Define the forwarder response statusCode (default: 200) + responseStatusCode: 204, + + // Define headers in the forwarder response - DISABLE CORS!! + responseHeaders: { + 'Content-Type': 'text/plain', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'OPTIONS, POST, GET', + 'Access-Control-Max-Age': 2592000, // 30 days + } + +}) + +server.listen(9000) + +const target = name => (req, res) => { + // Buffer the received payload + const data = [] + req.on('data', chunk => data.push(chunk)) + + req.on('end', () => { + console.log(`[${name}] Received: ${req.method} ${req.url}. Header "some-header"=${req.headers['some-header']}`) + console.log(`[${name}] Payload totals: ${data.length} chunks, ${Buffer.byteLength(Buffer.concat(data))} bytes`) + res.end() + }) +} + +// Start target servers +http.createServer(target('TARGET_1')).listen(9001) +http.createServer(target('TARGET_2')).listen(9002) + +// Send a request +console.log('[SCRIPT] Sending a request to the forwarder: POST /somepath') +const req = http.request({ + hostname: '127.0.0.1', + port: 9000, + path: '/somepath', + method: 'POST' +}, res => console.log(`[SCRIPT] forwarder replied with statusCode=${res.statusCode}`, 'headers', res.headers)) + +const data = fs.createReadStream('./testfile.3mb.test', { flags: 'r', highWaterMark: 16 * 1024 }) +data.pipe(req, { end: true }) + diff --git a/lib/Forwarder.js b/lib/Forwarder.js index 1f0c2a6..a8c02bd 100644 --- a/lib/Forwarder.js +++ b/lib/Forwarder.js @@ -24,7 +24,6 @@ module.exports = class Forwarder extends EE3 { targetHeaders: {}, targetRetry: { maxRetries: 0 }, // don't retry by default responseStatusCode: 200, - responseBody: 'OK', responseHeaders: { 'Content-Type': 'text/plain' } }, options) @@ -59,6 +58,7 @@ module.exports = class Forwarder extends EE3 { listen(...args) { const listener = (inc, res) => { + if (this.options.requestTimeout) { inc.setTimeout(this.options.requesTimeout) } @@ -79,7 +79,7 @@ module.exports = class Forwarder extends EE3 { this.emit('requestContents', inc, contents) this.forwardRequests(inc, contents) - this.respond(inc, res) + this.respond(inc, res, contents) }) inc.on('error', err => this.emit('requestError', err, inc)) @@ -158,7 +158,7 @@ module.exports = class Forwarder extends EE3 { return params } - respond(inc, res) { + respond(inc, res, contents) { Object.keys(this.options.responseHeaders).forEach(h => res.setHeader(h, this.options.responseHeaders[h])) this.emit('response', inc, res) @@ -170,7 +170,7 @@ module.exports = class Forwarder extends EE3 { res.writeHead(this.options.responseStatusCode) } - res.write(this.options.responseBody) + res.write(contents) res.end() }