Node.js没有读取HTML POST请求

时间:2015-02-17 18:33:59

标签: html node.js postman

我的node.js脚本正在接收来自Google Chrome扩展程序邮递员的HTML POST请求调用,但它没有看到键值对。继续给我

TypeError: Cannot read property 'username' of undefined
   at app.get.params.QueueUrl (/home/ec2-user/Outfitr/Server/index.js:45:53)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)
    at next (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/route.js:110:13)
    at Route.dispatch (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/route.js:91:3)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)
    at /home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:267:22
    at Function.proto.process_params (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:321:12)
    at next (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/index.js:261:10)
    at serveStatic (/home/ec2-user/Outfitr/Server/node_modules/express/node_modules/serve-static/index.js:59:14)
    at Layer.handle [as handle_request] (/home/ec2-user/Outfitr/Server/node_modules/express/lib/router/layer.js:82:5)

这是我的代码。如果您还有其他需要,请告诉我

app.post('/bscreateuser', function(request, response) {
  process.stdout.write("Attempted to create a --- BBBSSSSS ---- user  |  ");
  process.stdout.write("request is " + request.url);
  process.stdout.write("username is " + request.body.username);
  process.stdout.write("Password is " + request.body.password);
  bscreateUser(request.query.username, request.body.password);
  response.send('Success' );
});

function bscreateUser(username, password) {
  messageBody = 'create_user("' + username + '","' + password + '")';
  queueUrl = DAO_QUEUE_URL;
  // sys.puts("--- going for BS ---");
  sendSQSMessage(JSON.stringify(messageBody), queueUrl);
}

1 个答案:

答案 0 :(得分:3)

 bscreateUser(request.query.username, request.body.password);

应该是:

 bscreateUser(request.body.username, request.body.password);

将来避免这种情况的好方法是:

var body = request.body;
var username = body.username;
var password = body.password

然后在代码中使用var username和var password,更不容易出错,而且更具可读性!