Вопрос по json, express, node.js – Как использовать данные JSON POST в приложении Express

243

Error: User Rate Limit Exceeded

(
        {
        id = 1;
        name = foo;
    },
        {
        id = 2;
        name = bar;
    }
)

Error: User Rate Limit Exceeded

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

    console.log("Got response: " + response.statusCode);

    response.on('data', function(chunk) {
        queryResponse+=chunk;
        console.log('data');
    });

    response.on('end', function(){
        console.log('end');
    });
});

Error: User Rate Limit Exceeded

Ваш Ответ

4   ответа
1

req.body

headers: {"Content-Type": "application/json"}

Error: User Rate Limit Exceededtext/jsonError: User Rate Limit Exceeded{}Error: User Rate Limit Exceeded
360

responserequest.

responserequest

Content-Type: application/jsonbodyParserrequest.body

var express = require('express')
  , app = express.createServer();

app.use(express.bodyParser());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
  response.send(request.body);    // echo the result back
});

app.listen(3000);

$ curl -d '{"MyKey":"My Value"}' -H "Content-Type: application/json" http://127.0.0.1:3000/
{"MyKey":"My Value"}

Updated for Express 4+

npm install body-parser

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

var app = express();

app.use(bodyParser.json());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});

app.listen(3000);

Update for Express 4.16+

express.json()

var express = require('express');

var app = express();

app.use(express.json());

app.post('/', function(request, response){
  console.log(request.body);      // your JSON
   response.send(request.body);    // echo the result back
});

app.listen(3000);
console.log('request =' + JSON.stringify(request.body))
request.body.MyKey
Error: User Rate Limit Exceededconsole.log()Error: User Rate Limit Exceededutil.inspect()Error: User Rate Limit Exceededconsole.log("with request", request.body);
Error: User Rate Limit Exceeded
192

$ npm install body-parser

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

var app = express()

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res, next) {
  console.log(req.body) // populated!
  next()
})
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
10

        const res_data = JSON.parse(body);
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceededbody
Error: User Rate Limit Exceeded

Похожие вопросы