Home > Blockchain >  Express post request gives {}
Express post request gives {}

Time:09-30

So, when i make a Post request to the ExpressJS api with postman (localhost:8080?test=test), i should get this from the api: {test: test} or something right? But i get this: {}.

Heres my code: Api:

const express = require('express');
const http = require('http');
const path = require('path');
const app = express();

var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
app.use(cookieParser())
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json())


app.use(express.static(__dirname   '../../../src/'));

app.get('/*', (req,res) => res.sendFile(path.join(__dirname)));

const server = http.createServer(app);

server.listen(port,() => {
    console.log('Running...');
})

app.post('/', function(req,res){
    let data = JSON.stringify(req.body);
    console.log(data);
    res.send(data)
})

Can you help me?

Edit: Printscreen

CodePudding user response:

Inside post endpoint you are console loging req.body which is actually {}. According to the endpoint you are recieving data as qury params. Therefore you have to use req.query instead req.body to capture the data.

  • Related