NodeJS,express:将数据从一个页面发布到另一个页面

时间:2015-12-31 10:09:43

标签: javascript node.js express

我必须将一些数据从一个页面发布到另一个页面。我正在使用Node与NodeJS。

我写了以下代码:

var express = require('express');  
var app = express();

app.get('/', function (req, res) {
   res.sendFile( __dirname + "/index.html" );
});

app.get('/RegistrationPage', function (req, res) {
   res.sendFile( __dirname + "/RegistrationPage.html" );
});

app.post('/RegistrationSuccessPage', function (req, res) {
   var uname = req.body.username
   var pwd = req.body.pwd
   var emailAddress = req.body.email
   postData = uname+","+pwd+","+emailAddress
   console.log(postData);
   res.sendFile( __dirname + "/RegistrationSuccessPage.html",uname);
});

在导航到“RegistrationSuccessPage”时,uname,pwd,emailAddress的值将被打印为未定义。

我也使用了以下代码,但没有帮助:

app.post('/RegistrationSuccessPage', function (req, res) {
   var uname = req.query.username
   var pwd = req.query.pwd
   var emailAddress = req.query.email
   postData = uname+","+pwd+","+emailAddress
   console.log(postData);
   res.sendFile( __dirname + "/RegistrationSuccessPage.html",uname);
});

有人可以帮助我如何从文本框中获取值并将其发布到其他页面上。

HTML代码如下:

<body>
        <div>   
            <div align="center">
                <label>Please enter below details</label><br><br>
                <label>Username *: </label><input type="username" name="username"/><br><br>
                <label>Password *: </label><input type="password" name="pwd" /><br><br>         
                <label>Email Address *: </label><input type="email" name="email"><br><br>
                <br><br>
                <form action="/RegistrationSuccessPage" method="post"><input type="submit" value="Submit" /></form>
            </div>
        </div>
    </body> 

1 个答案:

答案 0 :(得分:1)

您将输入字段放在表单标记内。使用下面的代码

  <body>
    <div>   
        <div align="center">
          <form action="/RegistrationSuccessPage" method="post">
            <label>Please enter below details</label><br><br>
            <label>Username *: </label><input type="username" name="username"/><br><br>
            <label>Password *: </label><input type="password" name="pwd" /><br><br>         
            <label>Email Address *: </label><input type="email" name="email"><br><br>
            <br><br>
            <input type="submit" value="Submit" /></form>
        </div>
    </div>
</body>

现在尝试我可以工作