Answer the question
In order to leave comments, you need to log in
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}`);
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question