0
node.js/clusterとmongoose接続に問題があります。 私はmongooseを使用して同じデータベースに複数のWebサーバーを接続できないのでしょうか?クラスタでのマングース接続エラー
私のコードの抜粋があります:
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const helmet = require('helmet');
const mongoose = require('mongoose');
const http = require('http');
if (cluster.isMaster) {
const cpuCount = require('os').cpus();
cpuCount.forEach((cpu) => {
cluster.fork(); // fork web server
});
cluster.on('exit', (worker, code, signal) => {
... // log
cluster.fork(); // on dying worker, respawn
});
} else {
//express middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(helmet());
... // express and global config
// The mongoose connection dit not work with cluster
mongoose.connect(__DB_URL__); // where __DB_URL__ = 'mongodb://database/test'
... //server config
const server = http.createServer(app);
server.listen
}
私は(コードの最初の部分なし)cluster
を使用せずに、このサーバを実行することができるよ(またはcluster
でなく、マングースなしで... )と、cluster
を使用してこのサーバーを実行すると、労働者はクラッシュしなかったが、マングース接続!
これは、JSONのエラーです:
{ MongoError: failed to connect to server [database:27017] on first connect
at Pool.<anonymous> (/Users/...)
... // long path error
name: 'MongoError',
message: 'failed to connect to server [database:27017] on first connect' }
私は、クラスタの各フォークでマングースを使用して、本当にできる午前または別の解決策はありますか?
JSコードが本当に悩ませていなかった...それはモンゴデーモンだった: '( とにかくありがとう、私はあなたのコードを試してみました、それがうまく機能します –