0

I've read dussins of stackoverflow posts now but I can't get my bodyparser to work. In order to not complicate things I changed to the most simple thing I could imagine, which is the code below and involve just parsing one single route. Can anyone see a problem?

routes/login.js

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

router.get('/', (req, res) => {
        res.send('<form method="POST" action="'+ req.originalUrl +'"><input type="text" id="personalNumber" autocomplete="off"><input type="submit" value="Log in"></form>');
        res.end();
});

router.post('/', bodyParser.urlencoded({ extended: false }), (req, res) => {
        console.log('personal', req.body, req.personalNumber, res.body, req.headers);
        res.send('personal', req.body, req.personalNumber, res.body, JSON.stringify(req.headers));
        res.end();
});

module.exports = router;

server.js simplified

var express = require('express');
var app = express();

var login = require(__dirname + '/routes/login');
app.use('/login', login);

app.listen(80, () => {
        console.log('Listening on port 80');
});

Going to /login, typing anything in the input and submitting (triggering a post to /login) will give use the following console output:

Listening on port 80
personal {} undefined undefined { host: 'localhost',
  connection: 'keep-alive',
  'content-length': '0',
  pragma: 'no-cache',
  'cache-control': 'no-cache',
  origin: 'http://localhost',
  'upgrade-insecure-requests': '1',
  'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
  'content-type': 'application/x-www-form-urlencoded',
  accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
  referer: 'http://localhost/login',
  'accept-encoding': 'gzip, deflate',
  'accept-language': 'en-US,en;q=0.8,sv;q=0.6' }

Expected output would be for req.body to contain some information about the data from the form.

req.body = {
    personalNumber: 'inputData'
};
  • You should be more specific about what is happening. Demonstrate what the code does differently than you are expecting it to – nem035 Sep 21 '16 at 06:03
  • does your console display your expected result in the command prompt? – Beginner Sep 21 '16 at 06:04
  • ...or is [running on port 80](http://stackoverflow.com/questions/18947356/node-js-app-cant-run-on-port-80-even-though-theres-no-other-process-blocking-t) a problem? – nem035 Sep 21 '16 at 06:04
  • Updated the question. I hope it helps to solve the mystery. – user5018460 Sep 21 '16 at 14:57

2 Answers2

1

body-parser works properly when it is added in your main file before all the routes

So put the following code after creating the object of express in server.js file

var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
  extended: false
}));
abdulbarik
  • 6,101
  • 5
  • 38
  • 59
0
    var express = require('express');
    var router = express.Router();
    var bodyParser = require('body-parser');
    router.urlencoded({ extended: false });
    router.use(bodyParser.json());
    router.get('/', (req, res) => {
            res.send('<form method="POST" action="'+ req.originalUrl +'"><input type="text" id="username" autocomplete="off"><input type="submit" value="Log in"></form>');
            res.end();
    });

    router.post('/', (req, res) => {
            console.log('personal', req.body, req.personalNumber, res.body, req.headers);
            res.send('personal', req.body, req.personalNumber, res.body, JSON.stringify(req.headers));
            res.end();
    });

    module.exports = router;

Try this, It may work.

suyesh
  • 659
  • 1
  • 10
  • 19