bastel bastel - 1 year ago 83
Node.js Question

Node JS redirection after submiting a form

I have a html form for my node app and if it gets submitted it shows /code and the text but nothing more and i can't redirect to my Mainpage. How can i redirect after it gets submitted with a Timer to another page? I tried to do it in and clientsided but its not working

<form action="/code" method="post" >
<input type="text" name="code" id="code"
style="text-align: center"
placeholder="Enter Code" /> <br> <br> <input
type="submit" class="btn btn-success" name="submit" id="submit"
value=" authentifizieren " />

App.js'/code', function(req, res) {

var code = req.param("code");

res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code+');

I just want to redirect from /code back to my mainpage or any other website.

Answer Source

Redirects in Node JS:

res.writeHead(302, {
  'Location': ''

If you want to do it client-side, in Node JS:

res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code);
res.write('<script>setTimeout(function () { window.location.href = ""; }, 5000);</script>');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download