在docker上使用Express-React应用程序时出现以下错误。
我提到了
并且解决方案似乎无效。
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}`);
});
答案 0 :(得分:1)
让我们理解一下:
API服务器在docker容器中的默认端口5000
上侦听请求。
使用docker-compose
组成API服务,以在节点机器中为容器中的目标端口5000
发布端口3000
。
然后,将客户端服务器设置为向节点计算机中的端口5000
代理API请求。
很显然,容器中3000
上没有任何服务正在运行。
您可以通过在5000
docker-compose.yaml
来更正目标端口
ports:
- 5000:5000