2017-07-16 260 views
0

我正在使用http-proxy創建代理服務器,然後連接到包含服務器的API的Node,Express,Mongodb,React/Redux應用程序邏輯。Node.js ReverseProxy無法連接到API(Windows 10)

當試圖連接到我的API服務器通我收到了我的應用程序服務器代理這些錯誤:

enter image description here

enter image description here

我的MongoDB服務器與的WebPack一起運行,但試圖啓動時Node Api服務器,它會立即關閉,儘管它以前一直工作正常,直到我添加了會話代碼。我的直覺是它可能實際上是一個Windows問題(我的生活故事)。你可以在下面找到我的代理和API代碼。

我的節點代理:

var express = require('express'); 
var path = require('path'); 
var favicon = require('serve-favicon'); 
var logger = require('morgan'); 
//PROXY BELOW 
var httpProxy = require('http-proxy') 



var app = express(); 

//PROXY TO API 
const apiProxy = httpProxy.createProxyServer({ 
    target:'http://localhost:3001' 
}); 
app.use('/api', function(req,res){ 
    apiProxy.web(req,res); 
}) 
//END PROXY 

// view engine setup 
// app.set('views', path.join(__dirname, 'views')); 
// app.set('view engine', 'jade'); 

// uncomment after placing your favicon in /public 
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); 
app.use(logger('dev')); 
// app.use(bodyParser.json()); 
// app.use(bodyParser.urlencoded({ extended: false })); 
// app.use(cookieParser()); 
app.use(express.static(path.join(__dirname, 'public'))); 

app.get('*', function(req,res){ 
    res.sendFile(path.resolve(__dirname,'public','index.html')) 
}) 

// catch 404 and forward to error handler 
app.use(function(req, res, next) { 
    var err = new Error('Not Found'); 
    err.status = 404; 
    next(err); 
}); 

// error handler 
app.use(function(err, req, res, next) { 
    // set locals, only providing error in development 
    res.locals.message = err.message; 
    res.locals.error = req.app.get('env') === 'development' ? err : {}; 

    // render the error page 
    res.status(err.status || 500); 
    res.render('error'); 
}); 

module.exports = app; 

我的節點API服務器:

var express = require('express'); 

var cookieParser = require('cookie-parser'); 
var bodyParser = require('body-parser'); 
var logger = require('morgan'); 
const session = require('express-session') 
const MongoStore = require('connect-mongo')(session) 


var app = express(); 

// view engine setup 
// app.set('views', path.join(__dirname, 'views')); 
// app.set('view engine', 'jade'); 


app.use(logger('dev')); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use(cookieParser()); 

//APIS 

var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost:27017/bookshop'); 

var db = mongoose.connect('mongodb://localhost:27017/bookshop'); 
db.on('error',console.error.bind(console,'#MongoDB - connection error')) 
//SET UP SESSIONS must be after mongodb connection 

app.use(session({ 
    secret:'mySecretString', 
    saveUninitialized:false,//record session only if user adds product to cart 
    resave:false,//session wont be resaved if didnt change 
    cookie:{maxAge: 1000 * 60 * 60 * 24 * 2},//two days in milliseconds 
    store: new MongoStore({mongooseConnection: db,ttl:2 * 24 * 60 * 60})//time to leave 2 days 24 hours 60 minutes 60 seconds 
})) 
//SAVE SESSION CART API 

app.post('/cart',function(req,res){ 
    var cart = req.body 
    req.session.cart = cart 
    req.session.save(function(err){ 
     if(err){ 
      throw err 
     } 
     res.json(req.session.cart) 
    }) 
}) 

//GET SESSION CART API 

app.get('/cart', function(req,res){ 
    if(typeof req.session.cart !==undefined){ 
     res.json(req.session.cart) 
    } 
}) 

//END SESSIONS 


var Books = require('./models/books.js') 

///POST BOOKS API 
app.post('/books',function(req,res){ 
    var book = req.body; 

    Books.create(book,function(err,books){ 
     if(err){ 
      throw err 
     } 
     res.json(books) 
    }) 
}); 

//GET BOOKS 

app.get('/books',function(req,res){ 
    Books.find(function(err,books){ 
     if(err){ 
      throw err 
     } 
     res.json(books) 
    }) 
}) 

//DELETE BOOKS 
app.delete('/books/:_id',function(req,res){ 
    var query = {_id:req.params._id}; 

    Books.remove(query,function(err,books){ 
     if (err){ 
      throw err 
     } 
     res.json(books) 
    }) 
}) 

//UpdateBooks(Not used in app) - uses put request 
//check EB 


//END APIS 

app.listen(3001,function(err){ 
    if(err){ 
     return console.log('API SERVER ERROR', err) 
    } 
    console.log('API Server is listening on http://localhost:3001') 
}) 

回答

0

我的一部分愚蠢的假設。無關的Windows:

問題是這一行:

var db = mongoose.connect('mongodb://localhost:27017/bookshop'); 

當應的是這個:

變種DB = mongoose.connection;