嘿,有趣的是,你应该问这个问题。我几分钟前才写这个。我正在使用咖啡脚本变体,但它看起来是这样的。我还在使用colors.js 在控制台上提供漂亮的彩色反馈,但除此之外,这应该很好地捕捉到您的数据库错误而不会消失。
让我先说我不是专家,我几周前才开始学习这个。
mongoose = require "mongoose"
db_address = "localhost/nodesample-dev"
mongoose.connection.on "open", (ref) ->
console.log "Connected to mongo server!".green
mongoose.connection.on "error", (err) ->
console.log "Could not connect to mongo server!".yellow
console.log err.message.red
try
mongoose.connect("mongodb://#{db_address}")
db = mongoose.connection
console.log "Started connection on " + "mongodb://#{db_address}".cyan + ", waiting for it to open...".grey
catch err
console.log "Setting up failed to connect to #{db_address}".red, err.message
这是在编译的 JS 中:
var db, db_address, mongoose;
mongoose = require("mongoose");
db_address = "localhost/nodesample-dev";
mongoose.connection.on("open", function(ref) {
return console.log("Connected to mongo server!".green);
});
mongoose.connection.on("error", function(err) {
console.log("Could not connect to mongo server!".yellow);
return console.log(err.message.red);
});
try {
mongoose.connect("mongodb://" + db_address);
db = mongoose.connection;
console.log("Started connection on " + ("mongodb://" + db_address).cyan + ", waiting for it to open...".grey);
} catch (err) {
console.log(("Setting up failed to connect to " + db_address).red, err.message);
}