node js post不与邮递员合作

时间:2015-03-06 05:52:23

标签: node.js server postman

我正在尝试为AWS上的节点JS服务器创建一个服务器服务器(不确定它是多么相关),我正在使用Postman进行测试。出于某种原因,我的hello world函数,这只是一个GET工作完美,但我创建用户的POST一直给我这个错误:

TypeError: Cannot read property 'username' of undefined
    at app.get.params.QueueUrl (/home/ec2-user/Outfitr/Server/index.js:40:29)
    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)

这是我的代码:

var express = require("express")
var app = express();
var gm = require("gm");
var fs = require("fs");
var async = require("async");

var s3 = new AWS.S3();

// set the region for the AWS API requests
AWS.config.region = 'us-west-2';

var DAO_QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/275333356355/DAO-Queue';

app.set('port', (process.env.PORT || 8080 ));
app.use(express.static(__dirname + '/public'));

app.get('/', function(request, response) {
  response.send('Hello World (Finally)');
});

app.post('/create_user', function( request, response) {

   process.stdout.write("WTF");

   create_user( request.body.username, request.body.password, request.body.firstname,    request.body.lastname, request.body.gender, request.body.latitude,
    request.body.longitude, request.body.description, request.body.city,
    request.body.state_province, request.body.country, request.body.email,
    request.body.phone_number);

    response.send('IT WORKED!');
});

function create_user( username, password, firstname, lastname, is_brand, gender,
   description, city, state_province, country, birthday) {

  messageBody = {"username":username, "password":password, "firstname": firstname,
   "lastname":lastname, "is_brand":is_brand, "gender":gender, "city":city,
   "state-province":state_province, "country":country, "birthday":birtyhday};
   queueUrl = DAO_QUEUE_URL;
   sendSQSMessage( JSON.stringify(messageBody),queueUrl);
}

可能出现什么问题?

1 个答案:

答案 0 :(得分:3)

确保您在邮递员中使用x-www-form-urlencoded标签。我已经摔倒了好几次。

另外,请确保使用body-parser包。

var express = require("express")
var app = express();
var gm = require("gm");
var fs = require("fs");
var async = require("async");
var bodyParser = require('body-parser');

var s3 = new AWS.S3();

// set the region for the AWS API requests
AWS.config.region = 'us-west-2';

var DAO_QUEUE_URL = 'https://sqs.us-west-2.amazonaws.com/275333356355/DAO-Queue';

// Add the middleware
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.set('port', (process.env.PORT || 8080 ));
app.use(express.static(__dirname + '/public'));

所以我认为你得到了错误,因为没有中间件为你设置req.body对象,bodyParser应该这样做。