简体   繁体   中英

Express Accessing Angular Http Post Data

I have been having trouble accessing the data that is passed to express.js from angular http service.

I am using the http service :

$http.post('/someUrl', data, config).then(successCallback, errorCallback);

This is what I am doing in angular:

$http.post('/api/topic', {data : topic}).then(successCallback, errorCallback);

This is what I am doing on in express:

app.post('/api/topic', function(req,res){console.log(req.data);});

We are assuming the topic is an object recieved from angular's frontend.

{
    username : 'dan',
    topic : 'I want to learn more',
    description : 'Where can I learn more about web development',
    category : 'web development'
}

So my question is : How does the backend end using express access the angular http data field?

for POST requests you need body parser (get requests still access data)

var app = require('express')();
var bodyParser = require('body-parser');
var multer = require('multer'); // v1.0.5

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

app.post('/api/topic', function (req, res, next) {
  console.log(req.body);
  res.json(req.body);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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