简体   繁体   English

如何在Node.js服务器的反应中显示图像

[英]How to show images in react from node.js server

I have a record in mongodb which holds the file name of an image I want to show on the frontend ie: react. 我在mongodb中有一条记录,其中包含要在前端显示的图像的文件名,即:react。 The image name is portraits.jpg in mongodb. 图像名称在mongodb中为portraits.jpg。

Firstly, should I be saving the path name as well into mongo or just the image name ie: /img/portraits.jpg vs portraits.jpg 首先,我应该将路径名称也保存到mongo中还是仅保存图像名称,即: /img/portraits.jpg vs portraits.jpg

Secondly, how can I show the image in React? 其次,如何在React中显示图像?

I am getting the data from the database using axios and then sending the data to another component: 我正在使用axios从数据库中获取数据,然后将数据发送到另一个组件:

const { categories } = this.props;

{categories.map(category => (
  <Col md={6} className="mb-3" key={category._id}>
    <Figure className="cat-photo">
      <h1>{category.name}</h1>
      <img src={category.catimg} className="img-fluid" />
    </Figure>
  </Col>
))}

When I view the source code in browser, it is looking at http://localhost:3000/img/portraits.jpg but the node.js server is running on port 8000. So, I am guessing that is why there is no image showing up because the image is on the server and nowhere to be found in React file structure. 当我在浏览器中查看源代码时,它正在查看http://localhost:3000/img/portraits.jpg但是node.js服务器在端口8000上运行。因此,我猜测这就是为什么没有图像的原因之所以显示,是因为该图像位于服务器上,而在React文件结构中找不到该图像。

In the first place, make sure your image is actually accessible on the node server (eg visit http://localhost:8000/img/portraits.jpg ). 首先,请确保您的映像在节点服务器上实际上是可访问的(例如,访问http:// localhost:8000 / img / portraits.jpg )。

If it is, you just need to add the hostname to the URL. 如果是,则只需将主机名添加到URL。 You can do it old school like src={"http://localhost:8000" + category.catimg} or with new in string replacement as follows: 您可以使用src={"http://localhost:8000" + category.catimg}这样的旧src={"http://localhost:8000" + category.catimg}也可以使用如下所示的新字符串替换形式:

const { categories } = this.props;

{categories.map(category => (
  <Col md={6} className="mb-3" key={category._id}>
    <Figure className="cat-photo">
      <h1>{category.name}</h1>
      <img src={`http://localhost:8000${category.catimg}`} className="img-fluid" />
    </Figure>
  </Col>
))}

Regarding your question, if you should store the whole path into your database or not, that is something left to decide to you. 关于您的问题,是否应该将整个路径存储到数据库中,这取决于您。 It's not about what is better but what suits your architecture and use case better. 并不是什么更好,而是什么更适合您的体系结构和用例。 However, IMHO this is a very minor and irrelevant question and should be revertable at any point anyway. 但是, 恕我直言,这是一个非常小且无关紧要的问题,无论如何应该可以恢复。

To answer your first question, should you store the path in MongoDB, that depends. 要回答您的第一个问题,您应将路径存储在MongoDB中,具体取决于。 If all the images live in a flat namespace (ie, in a single directory), then there's no need to store the path in the database, since all the images will have the same path. 如果所有图像都位于一个平面的命名空间中(即,在一个目录中),则无需将路径存储在数据库中,因为所有图像将具有相同的路径。 But if the paths may vary, then you at least need to store some part of the path. 但是,如果路径可能有所不同,那么您至少需要存储路径的某些部分。

On to your next question. 关于您的下一个问题。 You have two servers: 您有两台服务器:

  • (Probably) Webpack, which is running on port 3000 and serving your React assets (可能)Webpack,它运行在端口3000上并为您的React资产提供服务
  • Your Node server on port 8000 您的节点服务器在端口8000上

Your UI is trying to load the image from Webpack because that's where the page came from. 您的UI试图从Webpack加载图像,因为那是页面的来源。 You need to configure Webpack to proxy service calls and other requests for dynamic assets to your Node server. 您需要将Webpack配置为将服务调用和其他对动态资产的请求代理到您的Node服务器。

A simple configuration: 一个简单的配置:

devServer: {
  ...
  proxy: {
    '/img': {
      target: 'http://localhost:3000',
      secure: false
    }
  }
}

There's a Medium article that goes into greater detail. 有一篇“ 中型”文章会更详细地介绍。

If you're using create-react-app and it is managing your Webpack configuration, you can put the proxy information in package.json . 如果您正在使用create-react-app并正在管理Webpack配置,则可以将代理信息放入package.json See the documentation for more information. 请参阅文档以获取更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM