VM服务器连接到另一个私有VM上的mongo数据库,但是所有请求都挂起

时间:2019-04-08 18:19:52

标签: database mongodb ubuntu mongoose

我们在VM上运行了一个服务器,该服务器成功连接到另一个正在运行mongo数据库实例的VM。

但是,每当我们尝试查询数据库或保存新文档时,请求就会永远挂起,甚至都不会收到错误消息。我们想弄清楚为什么会发生并解决它,以便我们可以实际使用我们的数据库。

但是我们可以在终端的mongo shell中发布帖子和查询。

我们的服务器

//imports
var express    = require('express');        // call Express for routing
var bodyParser = require('body-parser');    // call Body Parser to Parse JSON
var mongoose   = require('mongoose');       // call Mongoose to make calls to our MondoDB

// define our app using express & bodyparser
var app = express();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());


// connect to MongoDB
var db = mongoose.connect('mongodb://10.0.0.6:27017');

//Router & Routes-------------------
var router = express.Router();

//Set up middleware logging
router.use(function(req, res, next) {
    console.log('[REQUEST] ' + req.method + req.url); //Print request
    next(); // Continue processing request
});

router.use('/users', require('./routes/Users'));
router.use('/vimconfigurations', require('./routes/VimConfigurations'));
router.use('/vms', require('./routes/Vms'));
router.use('/vimevents', require('./routes/VimEvents'));

router.get('/', function(req, res) {
    res.json({ success: true, message: 'success' });  
});

//server start
app.use('/api', router);
process.env.PORT = 8081;
app.listen(process.env.PORT);
console.log('[USAGE SERVER] Initialized on port ' + process.env.PORT + "...");

路线示例

var express = require('express');
var router = express.Router();
var Vm = require('../models/Vm');

// routes for /api/vms
router.route('/')
    // Gets all vms
    .get(function(req, res){
        Vm.find(function(err, vms){
            if (err) return res.send(err);
            res.json(vms);
        });
    })

    // Make a new Vm
    .post(function(req, res){
        var vm = new Vm();
        vm.running = false;
        vm.creationdate = new Date();
        vm.configuration = configBasic;
        vm.ownerCcId = req.body.ccId;
        vm.save(function(err, vm){
            if (err) return res.send(err);
            res.json({success: true, message : 'VM successfully created!', id : vm._id});
        });
    });
module.exports = router;

示例模型


// server/models/vm.js

var mongoose     = require('mongoose');
var Schema       = mongoose.Schema;

var VMSchema   = new Schema({
    ownerCcId: String,
    running: Boolean,
    configuration: Object,
    creationdate: Date
});

module.exports = mongoose.model('Vm', VMSchema);

任何帮助将不胜感激,谢谢!

0 个答案:

没有答案