Node JS redirect after form submit

I have a html form for my node application and if it is submitted it shows / code and text but nothing else and I cannot redirect to the main page. How can I redirect after sending using a timer to another page? I tried to do this in app.post and clients but didn't work

<form action="/code" method="post" >
<fieldset>
<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 " />
</fieldset>
</form>

      

App.js

app.post('/code', function(req, res) {


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

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

      

I just want to redirect from app.post/code back to the main page or any other site.

+3


source to share


2 answers


Redirects in Node JS:

res.writeHead(302, {
  'Location': 'http://example.com/'
});
res.end();

      



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 = "http://example.com/"; }, 5000);</script>');
res.end();

      

+3


source


res.writeHead(200, {'Content-Type': 'text/html'});
res.write('Code: '+code);
res.write('<script>setTimeout(function () { window.location.href = "http://example.com/"; }, 5000);</script>');
res.end();

      



0


source







All Articles