我有3个节点服务器名为:
dishServer.js
var mongoose = require('mongoose'),
assert = require('assert');
var Dishes = require('./models/dishes');
// Connection URL
var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
// create a new dish
Dishes.create({
"name": "Uthapizza",
"description": "A unique . . .",
"comments": [
{
"rating": 5,
"comment": "Imagine all the eatables, living in conFusion!",
"author": "John Lemon"
},
]
}, function (err, dish) {
if (err) throw err;
console.log('Dish created!');
console.log(dish);
var id = dish._id;
// get all the dishes
setTimeout(function () {
Dishes.findByIdAndUpdate(id, {
$set: {
description: 'Updated Test'
}
}, {
new: true
})
.exec(function (err, dish) {
if (err) throw err;
console.log('Updated Dish!');
console.log(dish);
dish.comments.push({
rating: 5,
comment: 'I\'m getting a sinking feeling!',
author: 'Leonardo di Carpaccio'
});
dish.save(function (err, dish) {
console.log('Updated Comments!');
console.log(dish);
db.collection('dishes').drop(function () {
db.close();
});
});
});
}, 3000);
});
});
leaderServer.js
var mongoose = require('mongoose'),
assert = require('assert');
var Leaders = require('./models/leadership');
// Connection URL
var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
// create a new leader
Leaders.create({
"name": "Peter Pan",
"description": "Our CEO, Peter, . . ."
}, function (err, leader) {
if (err) throw err;
console.log('Leaders created!');
console.log(leader);
var id = leader._id;
// get all promotion dishes
setTimeout(function () {
Leaders.findByIdAndUpdate(id, {
$set: {
description: 'Updated Test'
}
}, {
new: true
})
.exec(function (err, leader) {
if (err) throw err;
console.log('Updated Leader!');
console.log(leader);
db.collection('leaders').drop(function () {
db.close();
});
});
}, 3000);
});
});
promoServer.js
var mongoose = require('mongoose'),
assert = require('assert');
var Promotions = require('./models/promotions');
// Connection URL
var url = 'mongodb://localhost:27017/conFusion';mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
// we're connected!
console.log("Connected correctly to server");
// create a new promotion
Promotions.create({
"name": "Weekend Grand Buffet",
"description": "Featuring . . ."
}, function (err, promotion) {
if (err) throw err;
console.log('Promotions created!');
console.log(promotion);
var id = promotion._id;
// get all promotion dishes
setTimeout(function () {
Promotions.findByIdAndUpdate(id, {
$set: {
description: 'Updated Test'
}
}, {
new: true
})
.exec(function (err, promotion) {
if (err) throw err;
console.log('Updated Promotion!');
console.log(promotion);
db.collection('promotions').drop(function () {
db.close();
});
});
}, 3000);
});
});
有没有办法将它们组合成一个引用这三个服务器的节点服务器? 我找到了通过引用三种模型来制作节点服务器的方法:菜肴,领导力,促销。但不能将这三个服务器合二为一。
帮助将不胜感激。
答案 0 :(得分:0)
您只想针对单个数据库执行数据库操作,每个数据库都涉及不同集合的逻辑。您可以连接到Mongo并使用任何代码工作流程控制依赖项,例如async
或练习,例如使用Promise
来执行您想要的操作。
它不是服务器您需要的东西,而是一次性执行不同的操作。我的意思是,它并不像你设置一个Web服务器来为远程数据库提供API,从你的代码来看,它们似乎是特定的db"操作"你正在表演。