通过http - node.js发送jpg的问题

时间:2022-05-30 21:18:54

I'm trying to write a simple http web server, that (among other features), can send the client a requested file.
Sending a regular text file/html file works as a charm. The problem is with sending image files.
Here is a part of my code (after parsing the MIME TYPE, and including fs node.js module):

我正在尝试编写一个简单的http web服务器(包括其他特性),它可以向客户机发送请求的文件。发送一个常规的文本文件/html文件是一种魅力。问题是发送图像文件。这里是我的代码的一部分(在解析MIME类型之后,包括fs节点)。js模块):

if (MIMEtype == "image") {    
    console.log('IMAGE');  
    fs.readFile(path, "binary", function(err,data) {  
        console.log("Sending to user: ");  
        console.log('read the file!');  
        response.body = data;  
        response.end();  
    });  
} else {
    fs.readFile(path, "utf8", function(err,data) {
        response.body = data ;
        response.end() ;
    });
}    

Why all I'm getting is a blank page, upon opening http://localhost:<serverPort>/test.jpg?

为什么在打开http://localhost: /test.jpg时,我得到的只是一个空白页面?

1 个解决方案

#1


3  

Here's a complete example on how to send an image with Node.js in the simplest possible way (my example is a gif file, but it can be used with other file/images types):

这里有一个关于如何发送带有节点的图像的完整示例。以最简单的方式(我的例子是一个gif文件,但是它可以与其他文件/图像类型一起使用):

var http = require('http'),
    fs = require('fs'),
    util = require('util'),
    file_path = __dirname + '/web.gif'; 
    // the file is in the same folder with our app

// create server on port 4000
http.createServer(function(request, response) {
  fs.stat(file_path, function(error, stat) {
    var rs;
    // We specify the content-type and the content-length headers
    // important!
    response.writeHead(200, {
      'Content-Type' : 'image/gif',
      'Content-Length' : stat.size
    });
    rs = fs.createReadStream(file_path);
    // pump the file to the response
    util.pump(rs, response, function(err) {
      if(err) {
        throw err;
      }
    });
  });
}).listen(4000);
console.log('Listening on port 4000.');

UPDATE:

更新:

util.pump has been deprecated for a while now and you can just use streams to acomplish this:

跑龙套。泵已经被弃用了一段时间了,你可以使用流来实现这一点:

fs.createReadStream(filePath).pipe(req);

#1


3  

Here's a complete example on how to send an image with Node.js in the simplest possible way (my example is a gif file, but it can be used with other file/images types):

这里有一个关于如何发送带有节点的图像的完整示例。以最简单的方式(我的例子是一个gif文件,但是它可以与其他文件/图像类型一起使用):

var http = require('http'),
    fs = require('fs'),
    util = require('util'),
    file_path = __dirname + '/web.gif'; 
    // the file is in the same folder with our app

// create server on port 4000
http.createServer(function(request, response) {
  fs.stat(file_path, function(error, stat) {
    var rs;
    // We specify the content-type and the content-length headers
    // important!
    response.writeHead(200, {
      'Content-Type' : 'image/gif',
      'Content-Length' : stat.size
    });
    rs = fs.createReadStream(file_path);
    // pump the file to the response
    util.pump(rs, response, function(err) {
      if(err) {
        throw err;
      }
    });
  });
}).listen(4000);
console.log('Listening on port 4000.');

UPDATE:

更新:

util.pump has been deprecated for a while now and you can just use streams to acomplish this:

跑龙套。泵已经被弃用了一段时间了,你可以使用流来实现这一点:

fs.createReadStream(filePath).pipe(req);