0

Is it possible to redirect the user to whatever site they put in a text box? This is my HTML code:

<form method="post">
    http://<input type="text" name="url">
    <input type="submit" value="Submit">
</form>

Node.JS code:

if(request.method=="POST")
{
request.on('data', function (chunk) {
  data = chunk.toString().replace("url=","")
  console.log("DATA: "+data)
});

request.on('end', function(){
  console.log("Done");
})

console.log("end")
response.writeHead(302, {'Location' : 'http://'+data})
}

the code prints out the data, done and then end sequentially. obviously it doesnt redirect the user. i already know how to redirect the user, but the code im using first tries to redirect the user, then reads what the user has written in.

kxait
  • 1
  • 1
  • 1
  • 2
    Possible duplicate of [Nodejs Redirect URL](http://stackoverflow.com/questions/11355366/nodejs-redirect-url) – Serge K. Dec 13 '15 at 21:31
  • Why not redirect the user on the end event? By then, you should have data ready. – Quy Dec 14 '15 at 00:49

1 Answers1

0

Your code is working fine,you missed response.end()

if(request.method=="POST")  
{
  request.on('data', function (chunk) {
  data = chunk.toString().replace("url=","");
   console.log("DATA: "+data)
});

  request.on('end', function(){
    console.log("Done");
 }) 
   response.writeHead(302, {'Location' : 'http://'+data});
      response.end();
     console.log("end");

   }
Naved Mir
  • 97
  • 2
  • 9