简体   繁体   English

通过POST将Angular变量传递给Express后端

[英]Passing Angular variable to Express backend through POST

req.body is always empty. req.body始终为空。 I'm not sure what I'm doing wrong? 我不确定自己在做什么错? I tried adding content-type headers as json but that didn't do anything either. 我尝试将内容类型标头添加为json,但也没有执行任何操作。 Can someone lead me in the correct direction please? 有人可以指引我正确的方向吗? Thank you 谢谢

EDIT: just for clarification purposes, my Angular frontend hits the backend function successfully, but req.body is empty. 编辑:仅出于说明目的,我的Angular前端成功命中了后端函数,但req.body为空。 If I understand everything correctly, if I'm using the 'body-parser' library, it should be passed in through post through 'req.body'. 如果我正确理解了所有内容,则如果我使用的是“ body-parser”库,则应通过“ req.body”将其通过post传递。 I'm just not seeing that though and I'm not sure why. 我只是没有看到,但我不确定为什么。

EDIT2: I have the body parser code in my app.js but the backend routing in a index.js file, does that have anything to do with it? EDIT2:我的app.js中有正文解析器代码,但是index.js文件中的后端路由,这和它有什么关系吗?

EDIT3: app.js http://pastebin.com/9vNgf0Nd index.js http://pastebin.com/icLa3e2X EDIT3:app.js http://pastebin.com/9vNgf0Nd index.js http://pastebin.com/icLa3e2X

ANGULAR FRONTEND 角质前缘

service.registerAccount = function(account) {
        console.log(account);  //account = { userName: 'test', password: 'hello' }
        return $http({
            method: 'POST',
            url: '/register',
            data: { account: account },
            headers: {'Content-Type': 'application/json'}
        });
    }

BACKEND (app.js) BACKEND(app.js)

var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

BACKEND (index.js) 后端(index.js)

var express = require('express');
var router = express.Router();    

router.post('/register', function(req, res) {
  console.log(req.body);
};

Please remove this line 请删除此行

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

Also, 也,

app.use(bodyParser.json());

have to be called before app.use('/', routes); 必须在app.use('/', routes);之前app.use('/', routes);

And make sure to add Content-Type: application/json to the request header 并确保将Content-Type:application / json添加到请求标头

What happens if you add the content type? 如果添加内容类型会怎样?

service.registerAccount = function(account) {
        console.log(account);  //account = { userName: 'test', password: 'hello' }
        return $http({
            method: 'POST',
            url: '/register',
            data: { account: account },
            headers: {
               'Content-Type': 'application/json'
            }
        });
    }

There is nothing wrong in the UI code. UI代码没有错。 Not sure what is router so you may try this or post the code for router. 不确定router是什么,因此您可以尝试此操作或发布路由器代码。

Try this (this works for me) or you can also use your router: 试试这个(这对我有用),或者您也可以使用路由器:

app.post('/register', function (req, res) {
  console.log(req.body);
  res.send('welcome, ' + req.body.account.username)

});

You are missing: 您不见了:

var app = express();
app.use(router);

If you want to user routers refers to following example: 如果要使用用户路由器,请参考以下示例:

UPDATE with full code: 使用完整代码进行更新:

var express = require('express');
var router = express.Router();    
var app = express();
app.use(router);
router.post('/register', function(req, res) {
  console.log(req.body);
};
app.route('/register')
  .post(function (req, res) {
    console.log(req.body);
    res.send('welcome, ' + req.body.account.username)
  })

Try this 尝试这个

var express = require('express');
var app = express();
var bodyParser = require('body-parser');

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

app.post('/register', function(req, res) {
  console.log(req.body);
});

var server = app.listen(8081, function () {

   var host = server.address().address
   var port = server.address().port

   console.log("Example app listening at http://%s:%s", host, port)
});

and then execute this from prompt: 然后从提示执行此操作:

$ curl localhost:8081/register -v --data "{\"name\":\"test\",\"password\":\"hello\"}" --header "Content-Type: application/json"

this works for me! 这对我有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM