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");
});