Node.js 发送二进制文件
我需要测试一个文件上传,不使用多部分表单数据Node.js 发送二进制文件,node.js,express,mocha.js,chai,Node.js,Express,Mocha.js,Chai,我需要测试一个文件上传,不使用多部分表单数据 it('Create node', () => { return chai.request(server) .post('/api/sensornodes') .set('Content-Type', 'application/json') .send(fs.readFileSync('test/manifest/sensor_nodes.json')) .then((re
it('Create node', () => {
return chai.request(server)
.post('/api/sensornodes')
.set('Content-Type', 'application/json')
.send(fs.readFileSync('test/manifest/sensor_nodes.json'))
.then((res) => {
expect(res.status).to.eql(204);
});
});
这个测试调用下面的端点,在这里我使用主体解析器来解析请求
public create(req: Request, res: Response, next: NextFunction) {
console.log("req body", req.body)
}
但是当我执行测试时,req.body
是这样一个对象,不是我所期望的
{
type: 'Buffer',
data:
[ 123,
10,
32,
32,
32,
...
]
}
当我通过Postman调用该端点时,一切都正常,在req.body
中我有.json的内容
我哪里错了?我缺少编码类型 我已经修好了
.send(fs.readFileSync('test/manifest/sensor\u nodes.json','utf8'))