[英]What is the easiest way to display an image once it is generated on a node.js web server (using express)?
我使用express 4框架在node.js中創建了一個小型Web服務器。 當用戶從主頁上提交圖像文件時,它通過multer中間件上傳到服務器,由python腳本處理,然后將生成的圖像保存在服務器上。
完成並處理完的圖像保存到服務器后,將其發送回客戶端的最簡單方法是什么?
方法1 :從nodejs流式傳輸
如果您打算將結果文件流式傳輸到客戶端,則可以執行以下操作
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);
作為mimetype,請從http://www.sitepoint.com/web-foundations/mime-types-complete-list/
方法2 :利用公共文件
但是我認為使用Express最好的方法是將生成的圖像保存到express的“ public”文件夾中,然后直接提供給客戶端。
<img src="server.com/public/generated.jpg">
我特別推薦此方法,因為當將Nodejs / express與反向代理(nginx或類似的代理)一起使用時,您可以直接將公共文件夾提供給客戶端,而無需通過nodejs堆棧傳遞請求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.