last day (15 days later) » 

9:40 PM
1
Q: express is not fetching react build folder

randalI'm trying to deploy a react + express app, but locally npm start does not fetch the react build that is within the client folder. I have already done a react build, so a build folder does exist within the client folder. folder structure full folder structure main.js import 'dotenv/conf...

 
well you should define the full path. Where is main.js in the file structure? if its not inside /client then you need to tell it to look inside /client/build to get the build instead of static on the root
 
let me make an edit to show full folder structure
 
aka something like app.use(express.static(path.join(__dirname, 'client/build'))); or put your build folder at the root and not in /client
 
i made an edit that shows the full folder structure.
 
randal, you aren't pointing at the right location. if you console.log(path.join(__dirname, 'build')) in main.js you would see its missing the client folder in the path :)
 
9:40 PM
ok i will try that
 
Read my comments again :) i put in an example of what should fix it I think
 
app.use(express.static(path.join(__dirname, 'client/build'))); points the client and build, so i guess ill use that. Im rewriting the code
im sorta confused, orginially my code was app.use(express.static(path.join(__dirname, 'client/build'))); how did you assume console.log(path.join(__dirname, 'build')) when the existing code was app.use(express.static(path.join(__dirname, 'client/build'))); ?
 
im not assuming it. its in your code. you have 2 places where you call it. meaning you are actually doing this app.use(express.static(path.join(__dirname, 'client/build'))); but before doing that you are also doing this app.use(express.static(path.join(__dirname, 'build')));
 
would be so kind enough to provide an answer. I'm confused at this point, in the meanwhile ill tinker with the code and see what happens. thanks.
 
Hey dude, probably easier to explain like this
if you look at this jsfiddle link, i put a comment around the areas im talking about jsfiddle.net/0153dapv
 
9:41 PM
ok ill look at it now.
 
youll notice you are trying to do static for those folders multiple times. and different paths even. Instead just do it once and do it on the right path
 
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'build')));
 
if its easier search for path.join(__dirname, 'build and also search for path.join(__dirname, 'client in your editor to see the spots where its used
 
so just use one or the other.
 
you shouldn't be doing app.use(express.static(path.join(__dirname, 'build')));
because that isn't the right path
 
9:44 PM
what should i use instead
 
build is not at the root of the project
so like whats this project called?
like the name of the app / parent folder
 
myexpressknex
fixed it
 
great
so this is what the path actually looks like.
 
i just did
lets see
// app.use(express.static(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'client/build')));
 
path.join(__dirname, 'build') = /myexpressknex/build
path.join(__dirname, 'client/build') = /myexpressknex/client/build
 
9:46 PM
app.use(express.static(path.join(__dirname, 'client/build')));
if(process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, 'client/build')));
  //
  app.get('*', (req, res) => {
    res.sendfile(path.join(__dirname = 'client/build/index.html'));
  })
}
//build mode
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname+'/client/public/index.html'));

})
and kept that the same
 
you should only do it once
 
if you could be so kind, can you provide the answer, and ill mark it.
I already fixed.
 
like why when NODE_ENV is production do you do it again?
that will mess up prod build probably
only need it once, and since ur always doing it right about this if statement.. if(process.env.NODE_ENV === 'production') {. then you just dont need to do it again in that if block :)
yea i can throw up an answer real quick! did that solve it?
 
so instead of doing this

app.use(express.static(path.join(__dirname, 'client/build')));
if(process.env.NODE_ENV === 'production') {
  app.use(express.static(path.join(__dirname, 'client/build')));
  //
  app.get('*', (req, res) => {
    res.sendfile(path.join(__dirname = 'client/build/index.html'));
  })
}
//build mode
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname+'/client/public/index.html'));

})
just do

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

})
right ?
cause that works
just by itself.
 
hmm
im trying to figure out what good that if statement is doing
it doesn't seem to do anything different than what you're already doing right?
 
9:50 PM
i just followed a tutorial to be honest
not really
ill just delete it, because it doesn't really matter.
the following works
 
```
app.use(express.static(path.join(__dirname, 'client/build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname+'/client/public/index.html'));
})
```
just that should be good enough i think
 
//build mode
app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname+'/client/public/index.html'));

})
 
without the check for production
 
yeahh
it works
 
nice
yea
 
9:54 PM
thanks John apperciate your time.
 
you weren't doing anything special / different in that if block for production.
haha no problem! happy to help!
ill write up a quick thing :)
 
sounds good.
 
thanks!
i'll throw you a few upvotes
cuz #reputation haha!
 

last day (15 days later) »