【发布时间】:2016-02-09 02:47:33
【问题描述】:
我正在尝试用 restify 发布一个原始的身体。我的接收端是正确的,当使用 POSTman 时,我可以发送一个原始 zip 文件,并且该文件是在服务器的文件系统上正确创建的。但是,我正在努力用 mocha 编写我的测试。这是我的代码,任何帮助将不胜感激。
我已经尝试过这种方法。
const should = require('should');
const restify = require('restify');
const fs = require('fs');
const port = 8080;
const url = 'http://localhost:' + port;
const client = restify.createJsonClient({
url: url,
version: '~1.0'
});
const testPath = 'test/assets/test.zip';
fs.existsSync(testPath).should.equal(true);
const readStream = fs.createReadStream(testPath);
client.post('/v1/deploy', readStream, function(err, req, res, data) {
if (err) {
throw new Error(err);
}
should(res).not.null();
should(res.statusCode).not.null();
should(res.statusCode).not.undefined();
res.statusCode.should.equal(200);
should(data).not.null();
should(data.endpoint).not.undefined();
data.endpoint.should.equal('http://endpointyouhit:8080');
done();
});
但文件系统上的文件大小始终为 0。我没有正确使用我的 readStream,但我不确定如何更正它。任何帮助将不胜感激。
请注意,我想流式传输文件,而不是在发送和接收时将其加载到内存中,文件可能太大而无法进行内存操作。
谢谢, 托德
【问题讨论】: