From the server comes a GET request of the form:

http://qwe.rt/?foo=bar 

It is necessary to extract the bar parameter from the GET request, and create a new GET request to send to the server. How to do this in Node.js? At the moment there is a server that listens to the port, accepts the request and displays it on the console.

 var http = require('http'); http.createServer(function(request, response) { console.log(request.url); response.end(); }).listen(8888); console.log("Server has started"); 

    1 answer 1

    For this, the HTTP module has a function http.request (options [, callback]) with a full example in the documentation. You just have to remake it under the GET request.

     http = require("http"); var options = { hostname: 'www.google.com', port: 80, path: '/upload', method: 'GET' }; var req = http.request(options, (res) => { console.log(`STATUS: ${res.statusCode}`); console.log(`HEADERS: ${JSON.stringify(res.headers)}`); res.setEncoding('utf8'); res.on('data', (chunk) => { console.log(`BODY: ${chunk}`); }); res.on('end', () => { console.log('No more data in response.') }) }); req.on('error', (e) => { console.log(`problem with request: ${e.message}`); }); req.write(""); req.end(); 

    There is a URL module for parsing the incoming request path.