tat*_*man 5 pipe stream node.js
对你们中的一些人来说很容易问题.我有一个API端点,它返回一个http响应,其Content-Type设置为图像类型 - image/jpeg,image/png等,如果没有图像给我带有我正在发送的ID的人,则返回404.
如果是404,我想发送位于文件系统中的no file png.我一直在尝试使用请求和管道,我没有找到一个类似的例子.人们总是从文件中流式传输.
它应该单独工作:
app.get('/api/customers/:id/image', function (req, res) {
request.get(util.format(config.apis.getImage, req.params.id)).pipe(res);
//res.sendfile(path.join(__dirname, '/static/img/user.png'));
});
Run Code Online (Sandbox Code Playgroud)
它确实将图像返回给浏览器但引发异常:
stream.js:94
throw er; // Unhandled stream error in pipe.
Error: Parse Error
at Socket.socketOnData(http.js:1556:20)
...
Run Code Online (Sandbox Code Playgroud)
无论哪种方式,我都想检查请求调用的响应,以防它是404情况.
我想我对于溪流来说太新了,什么不是.
有帮助吗?
谢谢!