S
S
saiasasd2022-04-20 13:50:31
Node.js
saiasasd, 2022-04-20 13:50:31

Node JS post request not working?

I'm trying to process a post request. I found a simple code, but for some reason the result is an empty object without the data that I sent

const express = require('express');

const PORT = 8000;

const app = express();
const urlencodedParser = express.urlencoded({extended: false});

app.listen(PORT, () =>{
   console.log(`Server works on port ${PORT}`);
});

app.get('/', (req, res) =>{
    res.send('server works, PORT is ' + PORT);
});

app.post("/pos", urlencodedParser, function (request, response) {
    if(!request.body) return response.sendStatus(400);
    console.log(request.body);
    response.send(`${request.body.userName} - ${request.body.userAge}`);
});


625fe55cad432450675357.png

625fe574ec3a5696696915.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WapSter, 2022-04-20
@saiasasd

const express = require('express');

const PORT = 8000;

const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: false}))

app.listen(PORT, () =>{
   console.log(`Server works on port ${PORT}`);
});

app.get('/', (req, res) =>{
    res.send('server works, PORT is ' + PORT);
});

app.post("/pos", function (request, response) {
    if(!request.body) return response.sendStatus(400);
    console.log(request.body);
    response.send(`${request.body.userName} - ${request.body.userAge}`);
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question