表格提交通过邮政请求express.js

时间:2017-02-25 12:40:01

标签: javascript node.js

您好我正在尝试制作一个小应用程序。我正在尝试提交表单的Post方法。当用户提交表单时,我想保存数据,然后重定向到页面以显示结果。目前我无法发布数据,虽然我也为节点中的express.js使用了body解析器模块。如果你能说明我们需要放置与app.post方法链接相同的表单操作链接。这是我的代码:

  //html
  <b>Add a New Employee</b><br><br>
  <form action="/addEmp" method="Post">
   First name:
   <input type="text" name="firstname" value="">
  <br><br>
  Last name:
 <input type="text" name="lastname" value="">
 <br><br>
<input type="submit" value="Submit">
 </form>

  //express.js file:
  var bodyParser = require("body-parser");
 app.use(bodyParser.urlencoded({ extended: true }));
 app.use(bodyParser.json());

  app.get('/', function(req, res) {
   res.render('home');
    });

   app.get('/lastName/Smith', function(req, res) {
   res.render('about_quote', 
    {quotes: employees.lookupByLastName('Smith') 
     });
   });

  app.get('/addEmployee', function(req, res) {
  res.render('addEmp');

});

app.post('/addEmp', function(req, res) {
employees.addEmployee(req.body.firstname, req.body.lastname);
res.redirect('/lastName/Smith');
});// this method is creating problem as it is not posting data.

0 个答案:

没有答案