W
W
wo0dpeker2020-07-20 22:26:00
JavaScript
wo0dpeker, 2020-07-20 22:26:00

How to get JSON from POST object NODE JS + EXPRESS?

There is a code:

const express = require('express')
const session = require('express-session');
const axios = require('axios')
const bodyParser = require("body-parser");
const app = express()
const port = 3000
 const sessionOptions = {
   secret: '123456',
   cookie: {
     maxAge:269999999999
   },
   saveUninitialized: true,
  resave:true
};

const urlcodedParser = app.use(bodyParser.urlencoded({ extended : false}));

app.use(session(sessionOptions));


app.get('/', (request, response) => {
    response.send('Hello from Express!')
})

app.post('/test', (rq,rs) =>{
    console.log(rq.body);
})


rq.body in this case is equal to {}

What should I do so that I get the object that I sent?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hzzzzl, 2020-07-20
@wo0dpeker

const bodyParser = require("body-parser");
const app = express()

// надо же подключить к аппу
// Body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

R
Rag'n' Code Man, 2020-07-20
@iDmitriyWinX

https://www.npmjs.com/package/body-parser
Press CTRL + F and type this: bodyParser.json([options]), there will be some useful information about JSON in body-parser

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question