Nodejs:读图片

optfile.js

//-------------optfile.js-------------------------
var fs = require('fs');
module.exports = {
	readImg: function (path, res) {
		fs.readFile(path, 'binary', function (err, filedata) {
			if (err) {
				console.log(err);
				return;
			} else {
				console.log("输出文件");
				//res.writeHead(200,  {'Content-Type':'image/jpeg'});
				res.write(filedata, 'binary');
				res.end();
			}
		});
	}
}
var http = require('http');
var optfile = require('./model/optfile2');
http.createServer(function (request, response) {
    //response.writeHead(200,  {'Content-Type':  'text/html;  charset=utf-8'});  
    response.writeHead(200, { 'Content-Type': 'image/jpeg' });
    if (request.url !== "/favicon.ico") {  //清除第2此访问  
        //response.write('hello,world');//不能向客户端输出任何字节  
        optfile.readImg('./view/pig.png', response);
        //------------------------------------------------  
        console.log("end");
        //response.end('hell,世界');//end在方法中写过  
    }
}).listen(8000);
console.log('Server  running  at  http://127.0.0.1:8000/');

猜你喜欢

转载自blog.csdn.net/u013101178/article/details/84497579