admin管理员组

文章数量:1327690

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 app.post and clientsided but its not working

<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 my mainpage or any other website.

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 app.post and clientsided but its not working

<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 my mainpage or any other website.

Share asked Sep 17, 2014 at 16:01 bastelbastel 511 silver badge9 bronze badges 2
  • Can you handle the redirect on the client side (through an ajax success method) and just return a res.send(200) from the post call on the server? Seems like that's the way to go. – Christopher Marshall Commented Sep 17, 2014 at 16:10
  • could you redirect from server? why not? – sites Commented Sep 17, 2014 at 16:16
Add a ment  | 

3 Answers 3

Reset to default 5

Redirects in Node JS:

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

Two types of redirection can be done in pure node js

1)Within the same site

   res.writeHead(301, {"Location": "/blog/thank-you"});
   return res.end();   

2)To an External website

   res.redirect("https://stackoverflow./");

本文标签: javascriptNode JS redirection after submiting a formStack Overflow