0

I'm having trouble when posting to /package. This issue is driving me bonkers, I'm rather new to Node & don't have a clue what I'm doing wrong here.

req.body() is empty when doing so, unless I remove the app.all part, which, if I do remove, breaks the formidable upload post.

Could someone point me in the right direction? Why is the app.all part of my code breaking my package post?

A huge thanks in advance. Truely appreciate all the help I can get.

var express = require('express'),
    routes = require('./routes'),
    http = require('http'),
    fs = require('fs'),
    util = require('util'),
    path = require('path'),
    formidable = require('formidable');

var app = express();

// the code below breaks the package post, but when removed, breaks my upload post.
app.all('/*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'HEAD, GET, POST, PUT, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

app.configure(function(){
    app.set('port', process.env.PORT || 3000);
    app.set('uploadFolder', 'uploads');
    app.set('views', __dirname + '/views');
    app.set('view engine', 'ejs');
    app.use(express.favicon());
    app.use(express.logger('dev'));
    app.use(express.methodOverride());
    app.use(express.cookieParser());
    app.use(express.session());
    app.use(express.bodyParser());
    app.use(app.router);
});

app.get('/', function(req, res){
    res.render('index');
});

app.post('/upload', function(req, res){
    var form = new formidable.IncomingForm();
    form.maxFieldSize = 1000 * 60 * 60 * 2;
    form.uploadDir = app.get('uploadFolder');
    form.keepExtensions = true;

    form.parse(req, function(error, fields, files) {
        if(error)
            return res.json({"success": false, "error": error});


        res.json({
            "success": true,
            "id": "FILE ID HERE"
        });
    });
});

app.post('/package', function(req, res){
    console.log(req.body);
    res.json({"hello":"world"});
});

http.createServer(app).listen(app.get('port'), function(){
    console.log("Express server listening on port 3000");
});
Sam Granger
  • 401
  • 5
  • 10
  • Try adding **Content-Length** to your **Access-Control-Allow-Headers** – levi Aug 08 '13 at 21:58
  • Thanks for the suggestion, tried the following but still no luck unfortunately: res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Content-Length"); – Sam Granger Aug 08 '13 at 22:08
  • possible duplicate of [Express.js req.body undefined](http://stackoverflow.com/questions/9177049/express-js-req-body-undefined) – levi Aug 08 '13 at 23:44

1 Answers1

2

There are 2 ways to fix this. First, the more correct option is turn your app.all handler into middleware, so instead of app.all('/*', function(req, res, next) {, put

app.use(function(req, res, next) {

That way next means "go to the next middleware", which will eventually hit the router. The problem with the app.all wildcard route is that it's a route and not a middleware, so when you call next that means "skip every other route and proceed to the next middleware" (which is typically the end of the middleware chain. However, you CAN actually make it work it you do next('route'); in your * handler instead of just next(). But using middleware is cleaner since that code will never send a response body, it makes more sense to be middleware rather than a route.

Peter Lyons
  • 142,938
  • 30
  • 279
  • 274
  • Thank you for this clarification. I'm no englishman, though. Would you mind to simplify your explanation as i can't understand what the heck i have to do in order to get this to work – user2422960 Feb 13 '14 at 09:01
  • Change the line that starts with `app.all('/*'` to be the line in my snippet. – Peter Lyons Feb 13 '14 at 15:09
  • http://stackoverflow.com/questions/21777370/express-route-handling-req-body-empty-on-some-routes – user2422960 Feb 14 '14 at 10:54