Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法使用node.js和express提供图像(png)_Node.js - Fatal编程技术网

无法使用node.js和express提供图像(png)

无法使用node.js和express提供图像(png),node.js,Node.js,我曾经研究过类似的问题,但还没有找到解决问题的方法。。。我已经设置了一条快速路线来提供图像,但我无法让它从存储图像的位置返回图像。请注意,我包含了一个允许来自任何来源的请求的语句。当我请求http://localhost:8080/images/x10.png我得到的响应是带有src的空图像元素="http://localhost:8080/images/x10.png而不是从http://ubuntubox.dev/images/x10.png,这是映像的实际位置,也是我最终传递给请求方法的路

我曾经研究过类似的问题,但还没有找到解决问题的方法。。。我已经设置了一条快速路线来提供图像,但我无法让它从存储图像的位置返回图像。请注意,我包含了一个允许来自任何来源的请求的语句。当我请求
http://localhost:8080/images/x10.png
我得到的响应是带有
src的空图像元素="http://localhost:8080/images/x10.png
而不是从
http://ubuntubox.dev/images/x10.png
,这是映像的实际位置,也是我最终传递给请求方法的路径。我错过了什么?谢谢

app.get('/images/*', function(req, res, path){
  var imagePath = req.url,
      url = 'http://ubuntubox.dev' + imagePath;

  request(url, function(error, response, img) {
    if(!error && response.statusCode === 200) {
      res.header('Access-Control-Allow-Origin', '*');
      res.writeHead(200, {'Content-Type': 'image/png' });
      res.end(img, 'binary');
    } else if(response.statusCode === 404) {
      res.status(404);
      res.type('txt').send('oops');
    }
  });
}).listen(8080, '127.0.0.1');

我刚刚在快车4里为自己弄明白了

app.get('/images/img1.png', function(req, res){
  res.sendFile('/Absolute/path/to/the/file/images/img1.png');
});
我已经回答了他认为有用的问题,但我仍然会想到另一种提供图像的方式(在这种情况下,我不会手动为每个文件编写路由,并将文件发送到浏览器)

由于您已经在使用express,只需直接将静态图像发送到服务器,您已经在
express.static

app.use(express.static('/Absolute/path/to/the/file/images/img1.png'));

使用express.static的好处是您只需将图像添加到您想要的静态文件夹中,express就会为您提供图像(无需添加任何代码)。

我不知道您是否仍然存在此问题,但是

您的问题的解决方案就是放置一个.pipe(res),它会将文件发送到响应

app.get('/images/*', function(req, res, path){
  var imagePath = req.url,
      url = 'http://ubuntubox.dev' + imagePath;

  request(url).pipe(res);
}).listen(8080, '127.0.0.1');

如果您希望以合理的方式提供图像和其他资产,并且不需要编写一百万条路由,请尝试以下方法:

  • 创建一个新文件夹“public”,并将所有资产移动到其中
  • 打开server.js并添加以下行:
  • app.use(express.static('public'))

    您的资产现在应该可用,如下所示:

    http://localhost:3000/images/kitten.jpg
    http://localhost:3000/css/style.css
    http://localhost:3000/js/app.js
    http://localhost:3000/images/bg.png
    http://localhost:3000/hello.html
    

    来源:

    我不确定是不是同样的情况

    但我的回答是:

    var path = require('path');
    var express = require('express');
    var app = express();
    
    var dir = path.join(__dirname, 'public');
    
    app.use('/public', express.static(dir));
    
    app.listen(3000, function () {
        console.log('Listening on http://localhost:3000/');
    });
    
    请注意这一行:

    app.use('/public',express.static(dir))

    您需要使用
    应用程序再次添加路径。请使用
    方法
    我不想添加此部分,但这是使其工作的唯一方法。
    如果没有它,它会一直响应“错误”,我无法访问此文件


    希望我能帮助您。

    我怀疑是
    request()有问题。
    。这是http.request()
    (如果是,则需要修复)还是您在别处定义的自定义函数?这没有用。。不能为每个图像写入路由。。比如img1。。img2。。等等,然后把那份文件寄过来。不幸的是,它对我不起作用。看起来它几乎和我的实现一样。我得到一个404,它在正确的位置,有正确的名字无法获取/images/the_image.png'虽然无法正常工作,但它比我的解决方案更优雅。。经过一些调整,它成功了
    app.get('/images/*',function(req,res){var-imgpth=req.url,img=root_-path+imgpth;res.sendFile(img);})