0

我正在尝试在 node.js 中创建一个网络服务器,它从 Wikipedia 下载图像并将其服务器放在页面上。我无法让它工作。我将我的代码粘贴在一个在线沙箱中:http ://runnable.com/UXWTyD3pTQ1RAADe 。

这是我的代码:

var http = require('http');
var fs = require('fs');

var fd = fs.open('name.jpeg', 'r+');

var options = {
    host:'upload.wikimedia.org',
    port:80,
    path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};

var server = http.createServer(function(req, res){
    res.writeHead(200, ['Content-Type', 'text/html']);
    http.get(options,function(res) {
      res.on('data', function (chunk) {
        fs.write(fd, chunk, 0, chunk.length, 0, null);
      });
      res.on('end',function(){
        fd.end();
        res.send("<img src='name.jpeg'></img>");
        res.end();
      });
    });
});

server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);

我不断遇到:

节点服务器.js

跑步...

fs.js:415                                                                                    
  binding.write(fd, buffer, offset, length, position, wrapper);                              
          ^                                                                                  
TypeError: Bad argument                                                                      
    at Object.fs.write (fs.js:415:11)                                                        
    at IncomingMessage.<anonymous> (server.js:18:12)                                         
    at IncomingMessage.EventEmitter.emit (events.js:96:17)                                   
    at IncomingMessage._emitData (http.js:359:10)                                            
    at HTTPParser.parserOnBody [as onBody] (http.js:123:21)                                  
    at Socket.socketOnData [as ondata] (http.js:1485:20)                                     
    at TCP.onread (net.js:404:27)   
4

1 回答 1

0

工作代码 - 保存图像文件:

/**Try to get an image from Wikipedia and return it**/

var http = require('http');
var fs = require('fs');

var options = {
  host:'upload.wikimedia.org',
  port:80,
  path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};

var server = http.createServer(function(req, res){
  res.writeHead(200, ['Content-Type', 'text/html']);
  http.get(options,function(imgRes) {
    imgRes.pipe(fs.createWriteStream('name.jpeg'));
    res.end("<html><img src='name.jpeg'></img></html>");
  });
});

server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);

您还需要 node-static ( http://www.sitepoint.com/serving-static-files-with-node-js/ ) 来提供静态文件name.jpeg

但另一种方法是手动完成:

var http = require('http');
var fs = require('fs');

var options = {
  host:'upload.wikimedia.org',
  port:80,
  path:'/wikipedia/commons/1/15/Jagdschloss_Granitz_4.jpg'
};

var server = http.createServer(function(req, res){
  if(req.url == '/name.jpeg') {
    res.writeHead(200, ['Content-Type', 'image/jpg']);
    try {
      var imgData = fs.readFileSync('name.jpeg');
      res.end(fs.readFileSync('name.jpeg'));
    } catch(err) {
      res.end();
    }
  }
  else {
    res.writeHead(200, ['Content-Type', 'text/html']);
    http.get(options,function(imgRes) {
      imgRes.pipe(fs.createWriteStream('name.jpeg'));
      res.end("<html><img src='name.jpeg'></img></html>");
    });
  }
});

server.listen(process.env.OPENSHIFT_NODEJS_PORT, process.env.OPENSHIFT_NODEJS_IP);
于 2013-04-22T22:16:52.277 回答