如何将图像从Express.js投放到React

时间:2018-11-25 06:43:48

标签: javascript html css reactjs express

我正在尝试从我的node.js上的公共目录中提供图像,并将后端后端表达为Reacts(特别是create-react-app)。

我有典型的文件结构

-public
   -images
      -image.jpeg

在我的客户端,我有一个带有这样相对路径的图像标签

<img src='/images/me.jpeg' alt="" />

我的express app.js看起来像这样

const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const app = express();

const contactRouter = require('./routes/contact');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(express.static(__dirname + "/public"));
app.use(express.static(path.join(__dirname, 'client/build')));

//route handlers

app.use('/contact', contactRouter);

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname + '/client/build/index.html'));
});


module.exports = app;

ala,我收到的图片没有任何运气。有什么想法吗?我不确定在开发时是否还需要其他代理。我已经尝试过了,这在我的src文件夹中喜欢

const proxy = require('http-proxy-middleware');

module.exports = function (app) {
    app.use(proxy('/images/*', { target: 'http://localhost:5000' }));
}

我感谢帮助人员!

0 个答案:

没有答案