[英]Error with multipart/form-data in express.js running on Azure
So I've got an express site running on Windows Azure. 所以我在Windows Azure上运行了一个快速站点。 I'm currently having problems submitting forms that are marked as enctype="multipart/form-data".
我目前在提交标记为enctype =“multipart / form-data”的表单时遇到问题。
The error I'm getting in the logs is: TypeError: Object # has no method 'tmpDir' 我在日志中得到的错误是:TypeError:Object#没有方法'tmpDir'
When running natively (initiated via node.exe) it works absolutely fine, its only when using the AzureEmulator or on Azure itself it fails. 当本地运行时(通过node.exe启动)它运行得非常好,只有在使用AzureEmulator或Azure本身时才会失败。
Now I expect this has something to do with Azure's infrastructure, but I'm wondering whether anyone has managed to work around this? 现在我希望这与Azure的基础设施有关,但我想知道是否有人设法解决这个问题?
So a multi-pronged issue here, I'll explain my findings as best as possible, please bear with me. 所以这里有一个多管齐下的问题,我会尽可能地解释我的发现,请耐心等待。
Connect uses node-formidable for its multipart form parsing, specifically the IncomingForm class. Connect使用node-formidable进行多部分表单解析,特别是IncomingForm类。 In the constructor of IncomingForm it sets the upload directory to be that of the parameter you pass in, or defaults to the Operating System's temp directory, defined by os.tmpDir().
在IncomingForm的构造函数中,它将上传目录设置为您传入的参数的目录,或者默认为操作系统的临时目录,由os.tmpDir()定义。 However, this method is missing from the Windows implementation of node's "os" module.
但是,节点的“os”模块的Windows实现中缺少此方法。
After reading copious posts, threads etc, I discovered that you should be able to get around this, you need to set the uploadDir property of the bodyParser. 在阅读了大量的帖子,线程等之后,我发现你应该能够解决这个问题,你需要设置bodyParser的uploadDir属性。
app.use(express.bodyParser({ uploadDir: 'path/to/dir' }));
However there is (at the time of writing) a bug in connect's implementation of multipart forms processing in that it creates an object of IncomingForm without passing any parameters into the constructor, and then setting the properties further down: 但是(在编写本文时)连接的多部分表单处理实现中存在一个错误,因为它创建了一个IncomingForm对象,而没有将任何参数传递给构造函数,然后进一步设置属性:
var form = new formidable.IncomingForm
, data = {}
, files = {}
, done;
Object.keys(options).forEach(function(key){
form[key] = options[key];
});
So I've forked both express & connect and updating the code to read as: 所以我分叉表达和连接并更新代码读作:
var form = new formidable.IncomingForm(options)
, data = {}
, files = {}
, done;
Object.keys(options).forEach(function(key){
form[key] = options[key];
});
You can find the forked versions here: not a shameless plug 你可以在这里找到分叉版本: 不是无耻的插件
Fix for Windows Environments (Azure web sites + node.js application). 修复Windows环境(Azure网站+ node.js应用程序)。
server.js: server.js:
Make sure it does not set an upload dir or tmp dir 确保它没有设置上传目录或tmp目录
app.use(express.bodyParser());
packages.json: packages.json:
Force node 0.10.21 or above: 强制节点0.10.21或以上:
"engines": { "node": "v0.10.24" }
Force express 3.4.8 or above: Force Express 3.4.8或以上:
"express": "3.4.8"
This should update your node to the fixed lib versions and the problem should be gone. 这应该将您的节点更新为固定的lib版本,问题应该消失。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.