Home > Net >  Express routes don't return data within Docker conatiner
Express routes don't return data within Docker conatiner

Time:07-01

When the app starts, the home page immediately makes a request to my googleRoute to retrieve some review data.

When ran locally and visiting localhost:3001 the app starts up and displays the data fine.

When ran via docker and visiting localhost:3001 the app starts up and the data is 'undefined' as if the route never returned any data back.

Below is my code...

Express App Index.js:

const express = require('express')
const awsRouter = require('./routes/aws-route')
const googleRouter = require('./routes/google-route')
const dotenv = require('dotenv');
const path = require('path');

const PORT = process.env.PORT || 3001;

const app = express();
// middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

//routes
app.use("/api/aws", awsRouter);
app.use("/api/google", googleRouter);
app.use(express.static(path.join(__dirname, '../react-app/build')));
  
app.get('/', (req,res) => {
  res.sendFile(path.join(__dirname, '../react-app/build/index.html'));
});

app.listen(PORT, () => {
  console.log(`Server listening on ${PORT}`);
});

My Dockerfile:

# pull official base image
FROM node:13.12.0-alpine AS ui-build

# set working directory
WORKDIR /app
COPY react-app/ ./react-app
RUN cd react-app && npm install && npm run build

FROM node:13.12.0-alpine AS server-build   
WORKDIR /root/
COPY --from=ui-build /app/react-app/build ./react-app/build
COPY express-app/package*.json ./express-app/
COPY express-app/index.js ./express-app/
COPY express-app/routes ./express-app/routes
RUN cd express-app && npm install

EXPOSE 3001

CMD [ "node", "./express-app/index.js" ]

CodePudding user response:

After adding log statements to my routes, I noticed that I was able to reach the routes just fine however my environment variables were undefined.

To fix this I used the --env-file flag as mentioned here to reference my environment variables from my .env file

  • Related