在开发中使用两个猫鼬作品但不生产

时间:2018-06-01 22:15:26

标签: node.js database mongodb mongoose routes

我正在尝试为我的应用设置两个不同的mongoose,因为一个将global,而另一个仅用于特殊的api,并不总是需要它。我在本地进行了设置并进行了测试,这似乎正在工作但是当我在production中进行测试时,所有保存都进入了special db

我目前正在使用pm2 eco在生产环境中进行设置

我目前的进步是......

在我的切入点index.js

我有

// skipping the codes for import express and others
require("./dbs/global");
const index = require('./routes');  // this uses the glboal db
const otherRoute = require('./routes/otherRoute');  // this uses the glboal db

const special = require('./routes/special');  // this should use the special db

这就是我global.js的样子

//Import the mongoose module
const mongoose = require("mongoose");

//Set up default mongoose connection, if no env then run the dev db
const mongoDB =
    process.env.MONGO_URL || dev.db.location";
mongoose.connect(mongoDB, {
    useMongoClient: true
});
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise;
//Get the default connection
const db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on("error", console.error.bind(console, "MongoDB connection error:"));

我的special.js看起来像......

// skipping the codes for import express and others
require('../../dbs/special-db');

// regular post method in this file
router.post('blah', (req, res) => {
});

我的special-db.js看起来非常像global.js

//Import the mongoose module
const mongoose = require("mongoose");

//Set up default mongoose connection
const mongoDB =
    process.env.SPECIAL_MONGO_URL || dev.db";
mongoose.connect(mongoDB, {
    useMongoClient: true
});
// Get Mongoose to use the global promise library
mongoose.Promise = global.Promise;
//Get the default connection
const db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on("error", console.error.bind(console, "MongoDB connection error:"));

module.exports = mongoose;

我的ecosystem.config.js

module.exports = {
    apps: [
        {
            script: "index.js",
            watch: true,
            env_staging: {
                NODE_ENV: "development",
            },
            env_production: {
                NODE_ENV: "production",
                MONGO_URL: "prod.dv",
                SPECIAL_MONGO_URL: "special.prod.dv",
            }
        }
    ]
};

我甚至尝试了console.log(process.env.SPECIAL_MONGO_URL)console.log(process.env.MONGO_URL),看起来也很好。

我在这里缺少什么? 提前感谢您的帮助。

0 个答案:

没有答案