2

I'm trying a simple Node.js + Express RESTful API example, and I've created a simple action to simulate a login request. Here is my code:

app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});
module.exports = app;

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res) {
  res.send('respond with a resource');
});

/* My Login */
router.post('/login', function(req, res){
    var data = {
        user : req.body.user,
        pass : req.body.pass
    };
    res.send(data);
});

module.exports = router;

I'm simulating a POST request with Postman (user = joe, pass = 123456), but I'm receiving an empty {} as response. Already tried this solution, without success. How can I capture POST params on Node.js? What is wrong?

UPDATED I've embed my app.js and users.js code for a better review.

7
  • That's weird, at the very least you should receive the object with empty values or an error message. Does your NodeJS output any error logs? Are you sure your routing path is working correctly?
    – Hless
    Oct 24, 2014 at 13:26
  • What is the version of Express that you are using? You need to use the bodyParser middleware. Oct 24, 2014 at 13:36
  • What middleware do you have set up before that router? Can you show that code too?
    – mscdex
    Oct 24, 2014 at 13:43
  • @Hless there aren't error messages. The route works fine, I can print a Hello World, for example. But req.body.user returns an undefined value.
    – bodruk
    Oct 24, 2014 at 13:44
  • Also, what does console.log(req.headers['content-type']) show?
    – mscdex
    Oct 24, 2014 at 13:44

1 Answer 1

2

The problem is that you're sending a multipart/form-data request but you are not using any middleware that handles those types of requests (body-parser in Express 4 only handles application/x-www-form-urlencoded and JSON requests).

So you'll need to use a multipart parsing module such as connect-busboy/busboy, multer (Express 3 bodyParser-like API using busboy), connect-multiparty/multiparty, formidable (the previously bundled Express 3 bodyParser middleware), or reformed (uses busboy).

3
  • I've installed the body-parser as decribed here, but now, I have the following message: Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
    – bodruk
    Oct 24, 2014 at 14:33
  • Like I mentioned, body-parser does not handle multipart, so that will not help you. Although if you're not uploading files, you could use body-parser with your Content-Type set to application/x-www-form-urlencoded.
    – mscdex
    Oct 24, 2014 at 15:30
  • Content-Type: application/x-www-form-urlencoded works great to me!
    – bodruk
    Oct 24, 2014 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.