Reputation: 49
I have a problem with redirection. I have a small nodejs/express app that should redirect to external URL with form's input values after submiting form.
index.html
<form method="POST" action="https://192.0.2.1/abc.html">
<input name="name"/>
<input name="email"/>
<button type="submit">
</form>
It's only goes to https://192.0.2.1/abc.html
without inputs' values. Please, help.
Upvotes: 2
Views: 1576
Reputation: 111306
I wrote a working example of what you're describing:
'use strict';
var host = '127.0.0.1', port = 3333;
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({extended: true}));
app.get('/', (req, res) => {
res.type('html').end(`
<form method="POST" action="http://${host}:${port}/abc.html">
<input name="name"/>
<input name="email"/>
<button type="submit">Submit</button>
</form>
<form method="POST" action="http://${host}:${port}/def.html">
<input name="name"/>
<input name="email"/>
<button type="submit">Submit</button>
</form>
`);
});
app.post('/abc.html', (req, res) => {
var name = req.body.name, email = req.body.email;
res.redirect(`http://${host}:${port}/ghi.html?name=${name}&email=${email}`);
});
app.post('/def.html', (req, res) => {
res.type('html').end(`
<p>Name: ${req.body.name}</p>
<p>Email: ${req.body.email}</p>
`);
});
app.get('/ghi.html', (req, res) => {
res.type('html').end(`
<p>Name: ${req.query.name}</p>
<p>Email: ${req.query.email}</p>
`);
});
app.listen(port, () =>
console.log(`Listening on http://${host}:${port}/`));
You need to install express
and body-parser
:
npm install express body-parser
And run with:
node app.js
The first form correctly passes the parameters via POST method. The POST /abc.html
handler has access to them and sends a redirect to a custom URL of the form: /ghi.html?name=xxx&email=yyy
- the target of the redirection displays those values in HTML and you can see them in the address bar as well.
The second form doesn't redirect but directly displays the parameters, so that you could better see what's going on. It's also useful to open the Developer Console in your browser and observe the Network activity while you are clicking the form buttons.
It's not entirely clear from your example what can be wrong in your code, but I hope that showing you an example code that works will help you diagnose the problem in your implementation.
What you have to keep in mind is that for GET requests with parameters passed in the query string, you access them as:
req.query.name
req.query.email
and for the POST requests with parameters passed in the request body, you have to use body-parser
middleware and then you can access them with:
req.body.name
req.body.email
The requests that take place here are:
aaa
for name and bbb
for email and submit the first form/abc.html
with name=aaa&email=bbb
in the body302 Found
response with Location: http://127.0.0.1:3333/ghi.html?name=aaa&email=bbb
header/ghi.html?name=aaa&email=bbb
200 OK
response with HTML including the parametersUpvotes: 1