[英]What is the easiest way to display an image once it is generated on a node.js web server (using express)?
I created a small web server in node.js using the express 4 framework. 我使用express 4框架在node.js中创建了一个小型Web服务器。 When a user submits an image file from the main web page, it uploads to the server via the multer middleware, is processed by a python script, and then the resulting image is saved on the server. 当用户从主页上提交图像文件时,它通过multer中间件上传到服务器,由python脚本处理,然后将生成的图像保存在服务器上。
What is the simplest way I can deliver the finished, processed image back to the client once it is done and saved on the server? 完成并处理完的图像保存到服务器后,将其发送回客户端的最简单方法是什么?
Method 1 : Streaming from nodejs 方法1 :从nodejs流式传输
If you mean streaming the resulting file to the client, you could do something like this 如果您打算将结果文件流式传输到客户端,则可以执行以下操作
var fileStream = fs.createReadStream(filename);
fileStream.on('error', function (error) {
response.writeHead(404, { "Content-Type": mimetype});
response.end("file not found");
});
fileStream.on('end', function() {
console.log('sent file ' + filename);
response.end("");
});
fileStream.pipe(response);
as mimetype, use one from here http://www.sitepoint.com/web-foundations/mime-types-complete-list/ 作为mimetype,请从http://www.sitepoint.com/web-foundations/mime-types-complete-list/
Method 2 : Leveraging to public files 方法2 :利用公共文件
But using express, I think the best way is that the resulting image is saved into express' "public" folder, and that is served right to the client. 但是我认为使用Express最好的方法是将生成的图像保存到express的“ public”文件夹中,然后直接提供给客户端。
<img src="server.com/public/generated.jpg">
I recommend this method specially because when using nodejs/express with a reverse proxy (nginx or similar), you can serve the public folder directly to the client, without passing the request through nodejs stack. 我特别推荐此方法,因为当将Nodejs / express与反向代理(nginx或类似的代理)一起使用时,您可以直接将公共文件夹提供给客户端,而无需通过nodejs堆栈传递请求。
See this for more info http://expressjs.com/starter/static-files.html 请参阅此以获得更多信息http://expressjs.com/starter/static-files.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.