1

I get the following error when using a express-react app on docker.

enter image description here

enter image description here

i referred to this

React Proxy error: Could not proxy request /api/ from localhost:3000 to http://localhost:8000 (ECONNREFUSED)

and the solution doesn't seem to work.

setupProxy.js

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

module.exports = function(app) {
  app.use(proxy('/auth/github', { target: 'http://[::1]:5000/' }))
  app.use(proxy('/api/users/auth/github', { target: 'http://[::1]:5000/' }))
  app.use(proxy('/api/users/', { target: 'http://[::1]:5000/', "secure": false }))
  app.use(proxy('/api/posts/', { target: 'http://[::1]:5000/' }))
}

Dockerfile

FROM node:8.10.0-alpine
EXPOSE 5000
COPY . /home/app
WORKDIR /home/app
RUN npm install
CMD ["npm", "start"]

docker-compose.yml

# docker-compose.yml
version: "3"
services:
  app:
    build: .
    restart: always
    depends_on:
      - postgres
    ports:
      - 5000:3000
  postgres:
    image: postgres:9.6.8-alpine
    expose:
      - 5432
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: user
      POSTGRES_DB: db

Package.json

{
  "name": "sequelize-demo",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client": "cd ./client && npm start ",
    "server": "nodemon app.js  --ignore client",
    "build": "echo 'hello build'",
    "start": "concurrently --kill-others   \"npm run server\" \"npm run client\""
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "async": "^2.6.1",
    "bcrypt": "^3.0.3",
    "body-parser": "^1.18.3",
    "concurrently": "^4.1.0",
    "cookie-parser": "^1.4.3",
    "cookie-session": "^2.0.0-beta.3",
    "cors": "^2.8.5",
    "crypto": "^1.0.1",
    "dotenv": "^6.2.0",
    "express": "^4.16.4",
    "express-flash": "0.0.2",
    "express-session": "^1.15.6",
    "jsonwebtoken": "^8.4.0",
    "morgan": "^1.9.1",
    "nodemailer": "^5.1.1",
    "nodemon": "^1.18.9",
    "passport": "^0.4.0",
    "passport-github": "^1.1.0",
    "passport-github2": "^0.1.11",
    "passport-jwt": "^4.0.0",
    "passport-local": "^1.0.0",
    "pg": "^7.8.0",
    "pg-hstore": "^2.3.2",
    "sequelize": "^4.42.0"
  }
}

app.js

var express = require('express');
var app = express();
var userRoute = require('./routes/users');
var postRoute  = require('./routes/posts');
var bodyParser = require('body-parser');
var logger = require('morgan');
var models = require('./models');
var User = require('./models/user');
var session = require('express-session');
var cookieParser = require('cookie-parser') ;
var cookieSession = require('cookie-session');
var dotenv = require('dotenv');
var env = dotenv.config();
var cors = require('cors');
const port = process.env.PORT || 5000;
const passport = require('passport');
const path = require('path');


// CORS Middleware
app.use(cors());

app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
app.use(cookieParser());
app.use(bodyParser());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false })); 


app.use(session({
  secret : 'nodeauthsecret',
  resave: false,
 saveUninitialized: true,

}));

app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport);
require('./config/passport-github')(passport);

app.use(function(req, res, next) {
  res.locals.user = req.user; // This is the important line
  console.log(res.locals.user);
  next();
});
// app.use(function(req, res, next) {
//   res.setHeader("Access-Control-Allow-Origin", "*");
//   res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
// });



app.use('/api/users', userRoute )

app.use('/api/posts',  postRoute )




app.listen(port, function() {
  console.log(`Server is running on ${port}`);
});
randal
  • 1,272
  • 3
  • 23
  • 48
  • Why are you publishing 5000 port and trying to connect to 3000? – deosha Feb 15 '19 at 07:01
  • because react uses port 3000, and port 5000 handles the api requests from express. This code works, if this isn't on docker. – randal Feb 15 '19 at 07:05
  • why using port as `3000`? – PPShein Feb 15 '19 at 07:07
  • sorry i updated my code its really `1] [HPM] Error occurred while trying to proxy request /api/users/user from localhost:5000 to http://localhost:5000/ (ECONNREFUSED) (https://nodejs.org/api/errors.html#errors_common_system_errors)` – randal Feb 15 '19 at 07:19
  • could you try `http://localhost:5000/api/users/user` in your browser? – PPShein Feb 15 '19 at 07:26

1 Answers1

3

Let's make sense of this:

The API server listens for requests on default port of 5000 in the docker container.

The API service is composed using docker-compose to publish port 5000 in the node machine for target port 3000 in the container.

Then, the client server is set up to proxy API requests to port 5000 in the node machine.

Clearly, there isn't any service running on 3000 in the container.
You can correct the target port by setting it to 5000 in docker-compose.yaml

  ports:
      - 5000:5000
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81