0

I'm currently using body-parser to try and receive POST data but my request body returns an empty object. I can't seem to figure out what the issue is. Does anyone know what the issue with my code might be?

Here's my code:

// Script intialized by Gulp
const path = require('path')
const bodyParser = require('body-parser')
const express = require('express')
const app = express()

// Import registration and login functionality from database.js
const database = require('./database')

// Configuration
let jsonParser = bodyParser.json()
app.use(jsonParser)
app.use(express.static(path.join(__dirname, '../../docs')))
app.use(express.static(path.join(__dirname, '../../src')))

app.set('port', process.env.PORT || 8080);
app.set('view engine', 'pug')
app.set('views', 'src/pages')

app.listen(app.get('port'))
console.log('Listening on port: ' + app.get('port'))

// Routing
app.get('/', function(req, res) {
    console.log('Welcome!')
    res.render('index');
})

app.get('/login', function(req, res) {
    console.log('Uhh, it looks like something went wrong here')
    res.end()
})

// Link with database.js to link username and password attributes
app.post('/login', jsonParser, function(req, res) {
    console.log(req)
    console.log(req.body)
    //database.login('ThisIsATestAccountBoi', 'TestPassword')
    //console.log('Username: ' + username + '\n' + 'Password: ' + password)
    res.end()
})
Jelani Thompson
  • 332
  • 4
  • 14
  • Code seems fine, how are you posting the data ? – adeneo Oct 28 '16 at 18:49
  • @adeneo I submit my form data with the DOM's .submit() method. – Jelani Thompson Oct 28 '16 at 19:13
  • Well, you're expecting JSON as the only data, as you've used the `jsonParser` directly in your route, so if you're submitting a regular form as `x-www-url-form-urlencoded` it's clearly not sending just JSON, is it – adeneo Oct 28 '16 at 19:16
  • @adeneo I actually used the enctype='application/json' to set the data so it doesn't actually have to be. That's just the default. Thanks for the suggestion though :P – Jelani Thompson Oct 28 '16 at 19:20
  • What do you mean it doesn't have to be? you have a route like `app.post('/login', jsonParser, function(req, res) {` where `jsonParser` is middleware that parses a JSON request object and populates `req.body`, if the data sent isn't purely JSON, the parser fails, and `req.body` is empty ? – adeneo Oct 28 '16 at 19:22
  • @adeneo Hmm, even when I just go with the typical url-encoded bodyParser it returns empty though. Do you think it could be an issue with my form? I removed the enctype. – Jelani Thompson Oct 28 '16 at 19:49

2 Answers2

0

See this answer: Node.js server: get body from POST request

Looks like they were missing the Content-Type header. Send the request with the header Content-Type = application/json

Community
  • 1
  • 1
Austin Miller
  • 34
  • 1
  • 4
  • Nah I don't think that was the issue. I used res.setHeader() with the appropriate arguments and it still returns an empty object. I'll keep searching though! – Jelani Thompson Oct 28 '16 at 19:13
  • Not in the server code. Set the header in the request you are sending (from whatever tool you are using ex: Postman) – Austin Miller Oct 28 '16 at 20:11
0

Figured out my issue. It was an issue with my Jade tabbing. The way I had it tabbed made it so that my form closed on itself without having any components inside of it. Sorry for the inconvenience!

Jelani Thompson
  • 332
  • 4
  • 14