无法使用正文解析器获取/错误消息

时间:2017-02-15 00:20:14

标签: node.js express body-parser

我对节点很新,所以请原谅我的简单,但我不相信我正在抓住基本面。我正在尝试使用bodyparser提交表单,我收到错误消息:GET / newpoll?pollName =& option1 =& option2 =& submitButton =我提交后。

server.js

 'use strict';

 var express = require('express');
 var routes = require('./app/routes/index.js');
 var session = require('express-session');
 var bodyParser = require('body-parser');

 var app = express();


 app.use(bodyParser.urlencoded({ extended: true }));
 app.use(bodyParser.json());

 routes(app, passport);

 var port = process.env.PORT || 8080;
 app.listen(port,  function () {
   console.log('Node.js listening on port ' + port + '...');
 });

index.js        'use strict';

  var path = process.cwd();
  var ClickHandler = require(path +  '/app/controllers/clickHandler.server.js');   
  var bodyParser = require('body-parser');

module.exports = function (app, passport) {

app.route('/newpoll')
    .post(isLoggedIn, bodyParser, function(req, res) {
        //console.log(req.pollName);
        console.log(req.option1);
        console.log(req.option2);

    });

 };

form action =“/ newpoll”。我尝试在路由器中没有bodyparser变量,但它没有识别变量,我以为会这样?

1 个答案:

答案 0 :(得分:1)

GET请求不存在端点/newpoll,您必须发出POST请求才能访问数据,但是,如果要使用GET请求访问数据,则可以创建使用get方法的路线:

app.route('/newpoll')
  .get(isLoggedIn, function(req, res) {
     console.log(req.query);
  })
  .post(isLoggedIn, bodyParser, function(req, res) {
    console.log(req.body);
  })