POSTMAN中POST请求发送的参数始终未定义为express中的值

时间:2015-10-23 09:54:32

标签: post express postman

我有一个节点表达应用程序(我试图制作一个休息api),它有一个视图index.ejs,其中包含用户名,名称和消息字段(作为输入)的表单,每次表单都是提交,请求成功并将新用户添加到数据库中(我使用redis,但这对此问题并不重要)。 问题是,当我尝试通过POSTMAN使用相同的参数发出请求时,它总是失败(我尝试将json对象插入到主体原始板中,当它失败时我也尝试使用x-www-form-urlencoded但这些值在服务器的路由器中始终未定义,用于req.body.username,req.body.name)。

插入的json对象是:

{
    "username" : "shahid85@codeforgeek.com",
    "message" : "shahid message shahid message shahid message shahid message",
    "name" : "simple"
}

我试图找到像force the content-type to json这样的解决方案,但它也没有用。

这是我的app.js

var express = require('express'),
app = express(),
redis = require('redis'),
config = require('./config'),
client = redis.createClient(config.redisConf),
flow = require('flow-maintained'),
//  https://www.npmjs.com/package/flow-maintained
bodyParser = require('body-parser');

/***************************/

client.auth('redistest1');

client.on("error", function (err) {
console.log("Error " + err);
});

client.on('connect', function() {
console.log('connected to redis!!');

});


/*******************************/
app.set('view engine', 'ejs');
/*app.use(function(req, res, next) {
    // IE9 doesn't set headers for cross-domain ajax requests
    if(typeof(req.headers['content-type']) === 'undefined'){
        req.headers['content-type'] = "application/json; charset=UTF-8";
    }
    next();
})
.use(bodyParser.urlencoded({extended: false}));*/

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



app.get('/', function(req, res){
  GetMessages(function(messages){       
    res.render('index', {messages: messages});
  });
});



app.get('/messages', function(req, res){
  GetMessages(function(messages){
    console.log(' GetMessages is running !!!!');
    console.log('log-code 0003 :: messages.toString() : ' + messages.toString());
    console.log('log-code 0004 :: messages : ' + messages);
    res.json({messages: messages});     
  });
});

app.get('/messages/:id', function(req, res){
    var msgid = req.params.id;
    console.log('log-code 0000 :::     msgid : '+ msgid);

    FetchMessage(msgid, function(msg){
        console.log('log-code 0001 :::     message:'+ msgid+' : '+ msg);
        res.json({message: msg});
    });



});


app.post('/', function(req, res){

    console.log('log-code 0012 ---:::     id:'+ req.body.id);
    console.log('log-code 0013 ---:::     username: '+ req.body.username);
    console.log('log-code 0014 ---:::     name : '+ req.body.name);
    console.log('log-code 0015 ---:::     message:'+ req.body.message);

  var username = req.body.username;
  var name = req.body.name;

  GetUserID(username, name, function(userid){ // GetUserID va chercher si un "user:username" existe déjà
                                              // dans la base, si pas trouvé elle crée un nouvel 
                                              //  key ""user:username"" --> value "userid" (en passant 
                                              //  "user" et "username" à CreateUser() qui elle va incrémenter 
                                              //  "next:user:id" et l'utilise comme userid du nouvel "user:username" créé)
    AddMessage(req.body.message, userid, function(messid){
      console.log('Added message: ' + messid);
      res.redirect('/');
    });
  });
});

app.post('/messages', function(req, res){

    console.log('log-code 0018 ---:::     id:'+ req.body.id);
    console.log('log-code 0016 ---:::     username: '+ req.body.username);
    console.log('log-code 0017 ---:::     name : '+ req.body.name);
    console.log('log-code 0018 ---:::     message:'+ req.body.message);
    console.log('log-code 0018 ---:::     id:'+ req.body.id);

  var username = req.body.username;
  var name = req.body.name;

  res.write('\n Bien le bonjour');
  res.end();

});

function CreateUser(username, name, cb){
  client.incr('next:user:id', function(err, userid){
            /*
                Returned value userid is the Integer reply: the value of key after the increment
                see : http://redis.io/commands/incr
            */
    flow.exec(
      function(){
        var user_string = 'user:' + userid;
        client.set('user:' + username, userid, this.MULTI());
        client.hset(user_string, 'name', name, this.MULTI());
        client.hset(user_string, 'username', username, this.MULTI());
      },function(args){
        cb(userid);
      }
    );
  });
};

function GetUserID(username, name, cb){
  client.get('user:' + username, function(err, userid){
    if(userid){
      cb(userid);
    }else{
      CreateUser(username, name, function(new_user){
        cb(new_user);
      });
    }
  });
};

function AddMessage(message, userid, cb){
  client.incr('next:message:id', function(err, id){
    flow.exec(
      function(){
        var mess_id = 'message:' + id;
        client.set(mess_id, message, this.MULTI());
        client.set(mess_id + ':user', userid, this.MULTI());
        client.lpush('messages', id, this.MULTI());
      },function(){
        cb(id);
      }
    );
  });
};


function FetchMessage(id, cb){



  /********************************************/
   // conversion du code en-dessus au flow maintained
   var msg;
    flow.exec(
      function(){
        client.get('message:' + id, this);          
      },function(err, message) {

        msg = message;

        client.get('message:' + id + ':user', this);        
      },function(err, userid) {

        client.hget('user:' + userid, 'name', this);        
      },function(err, name){

        cb({message: msg, name: name});
      }
    );

    /*********************************************/     

};

function GetMessages(cb){
  flow.exec(
    function(){
      client.lrange('messages', 0, -1, this);
    },function(err, messages){
      //an async foreach
      var final_messages = [];
      flow.serialForEach(messages, function(el){
        FetchMessage(el, this);
      },function(mess){
        console.log('log-code 0005 :: mess to be added : ' + mess.toString());
        final_messages.push(mess);
      },function(){
        cb(final_messages);
      });
    }
  );
};


app.listen(8003);

enter image description here

1 个答案:

答案 0 :(得分:0)

单击位于窗口右上角蓝色提交按钮左侧的“参数”按钮,输入您的键值对。这应该有用。