发布到 /package 时遇到问题。这个问题让我发疯,我对 Node 很陌生,不知道我在这里做错了什么。
这样做时 req.body() 是空的,除非我删除了 app.all 部分,如果我删除了这部分,则会破坏强大的上传帖子。
有人能指出我正确的方向吗?为什么我的代码的 app.all 部分会破坏我的包裹帖子?
提前非常感谢。非常感谢我能得到的所有帮助。
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");
});