How, in Node.js, redirect the GET query to the new GET and send it to the server?



  • From the server, GET receives a species request:

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

    The parameter should be removed. bar from the GET request, and make a new GET request for the server. How do you do it in Node.js? At this point, there's a server that listens to the port, accepts the request and takes it to the console.

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

    console.log("Server has started");



  • For that, https://nodejs.org/api/http.html is a function. https://nodejs.org/api/http.html#http_http_request_options_callback with a full example in the documentation. All you have to do is redo it under GET.

    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's a way for the password. https://nodejs.org/api/url.html


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2