How to serve an image from Express.js to React





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am trying to serve up an image from my public directory on my node.js + express backend to the client side which is Reacts (create-react-app to be particular).



I have the typical file structure of



-public
-images
-image.jpeg


On my client side, I have an image tag with a relative path like so



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


My app.js for express looks like so



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;


alas, I am not getting any luck with the image being served up. Any ideas for this? I am not sure if I also need an additional proxy for when I am in dev. I have tried this which likes in my src folder in react



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

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


I appreciate the help guys!










share|improve this question




















  • 1





    Does your server have a build directory alongside your public folder like /dist?

    – Shawn Andrews
    Nov 25 '18 at 7:05











  • No it does not have one. What specifically would the build folder do?

    – Scott Selke
    Nov 25 '18 at 7:11






  • 1





    Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

    – Shawn Andrews
    Nov 25 '18 at 7:16











  • Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

    – Scott Selke
    Nov 25 '18 at 7:20






  • 1





    Ahh, im not familiar with proxies but everything else looks setup correctly

    – Shawn Andrews
    Nov 25 '18 at 7:55


















0















I am trying to serve up an image from my public directory on my node.js + express backend to the client side which is Reacts (create-react-app to be particular).



I have the typical file structure of



-public
-images
-image.jpeg


On my client side, I have an image tag with a relative path like so



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


My app.js for express looks like so



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;


alas, I am not getting any luck with the image being served up. Any ideas for this? I am not sure if I also need an additional proxy for when I am in dev. I have tried this which likes in my src folder in react



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

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


I appreciate the help guys!










share|improve this question




















  • 1





    Does your server have a build directory alongside your public folder like /dist?

    – Shawn Andrews
    Nov 25 '18 at 7:05











  • No it does not have one. What specifically would the build folder do?

    – Scott Selke
    Nov 25 '18 at 7:11






  • 1





    Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

    – Shawn Andrews
    Nov 25 '18 at 7:16











  • Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

    – Scott Selke
    Nov 25 '18 at 7:20






  • 1





    Ahh, im not familiar with proxies but everything else looks setup correctly

    – Shawn Andrews
    Nov 25 '18 at 7:55














0












0








0








I am trying to serve up an image from my public directory on my node.js + express backend to the client side which is Reacts (create-react-app to be particular).



I have the typical file structure of



-public
-images
-image.jpeg


On my client side, I have an image tag with a relative path like so



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


My app.js for express looks like so



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;


alas, I am not getting any luck with the image being served up. Any ideas for this? I am not sure if I also need an additional proxy for when I am in dev. I have tried this which likes in my src folder in react



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

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


I appreciate the help guys!










share|improve this question
















I am trying to serve up an image from my public directory on my node.js + express backend to the client side which is Reacts (create-react-app to be particular).



I have the typical file structure of



-public
-images
-image.jpeg


On my client side, I have an image tag with a relative path like so



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


My app.js for express looks like so



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;


alas, I am not getting any luck with the image being served up. Any ideas for this? I am not sure if I also need an additional proxy for when I am in dev. I have tried this which likes in my src folder in react



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

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


I appreciate the help guys!







javascript html css reactjs express






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 7:41









Yashwardhan Pauranik

2,24311731




2,24311731










asked Nov 25 '18 at 6:43









Scott SelkeScott Selke

327




327








  • 1





    Does your server have a build directory alongside your public folder like /dist?

    – Shawn Andrews
    Nov 25 '18 at 7:05











  • No it does not have one. What specifically would the build folder do?

    – Scott Selke
    Nov 25 '18 at 7:11






  • 1





    Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

    – Shawn Andrews
    Nov 25 '18 at 7:16











  • Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

    – Scott Selke
    Nov 25 '18 at 7:20






  • 1





    Ahh, im not familiar with proxies but everything else looks setup correctly

    – Shawn Andrews
    Nov 25 '18 at 7:55














  • 1





    Does your server have a build directory alongside your public folder like /dist?

    – Shawn Andrews
    Nov 25 '18 at 7:05











  • No it does not have one. What specifically would the build folder do?

    – Scott Selke
    Nov 25 '18 at 7:11






  • 1





    Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

    – Shawn Andrews
    Nov 25 '18 at 7:16











  • Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

    – Scott Selke
    Nov 25 '18 at 7:20






  • 1





    Ahh, im not familiar with proxies but everything else looks setup correctly

    – Shawn Andrews
    Nov 25 '18 at 7:55








1




1





Does your server have a build directory alongside your public folder like /dist?

– Shawn Andrews
Nov 25 '18 at 7:05





Does your server have a build directory alongside your public folder like /dist?

– Shawn Andrews
Nov 25 '18 at 7:05













No it does not have one. What specifically would the build folder do?

– Scott Selke
Nov 25 '18 at 7:11





No it does not have one. What specifically would the build folder do?

– Scott Selke
Nov 25 '18 at 7:11




1




1





Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

– Shawn Andrews
Nov 25 '18 at 7:16





Not a problem. Theoretically though your code is correct and the image request should stop at the line app.use(express.static(__dirname + "/public")); and return the image if one exists at /public/images/me.jpeg

– Shawn Andrews
Nov 25 '18 at 7:16













Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

– Scott Selke
Nov 25 '18 at 7:20





Hmm curious. If i were to look in the chrome dev tools, should I see a request being made to localhost:5000/public/images/me.jpeg from my <img> tag?

– Scott Selke
Nov 25 '18 at 7:20




1




1





Ahh, im not familiar with proxies but everything else looks setup correctly

– Shawn Andrews
Nov 25 '18 at 7:55





Ahh, im not familiar with proxies but everything else looks setup correctly

– Shawn Andrews
Nov 25 '18 at 7:55












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465279%2fhow-to-serve-an-image-from-express-js-to-react%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465279%2fhow-to-serve-an-image-from-express-js-to-react%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Academy of Television Arts & Sciences

L'Équipe

1995 France bombings