有人知道如何在使用MongoDb框架时连接到Hapi.js框架吗?
我只找到了一个例子(https://github.com/Marsup/hapi-mongodb),但这需要使用插件,代码中没有注释!
有谁知道更简单的方法吗?
发布于 2014-08-13 22:20:16
关于使用护照和猫鼬进行用户身份验证的博客文章
也要注意,Hapi的模型是基于插件的,所以在构建您自己的插件时,请阅读并重新读取文档。
发布于 2014-12-20 04:11:57
以下(使用猫鼬)对我非常有用:
var Hapi = require('hapi');
var mongoose = require("mongoose");
var server = new Hapi.Server();
server.connection({ port: 3000 });
var dbUrl = 'mongodb://localhost:27017/mydb';
var dbOtions = {
db: { native_parser: true },
server: { poolSize: 5 }
};
server.register([ /* plugins */], function (err) {
if (err) {
throw err; // something bad happened loading the plugins
}
// ... Register the routes
server.start(function () {
mongoose.connect(dbUrl, dbOtions, function(err) {
if (err) server.log('error', err);
});
});
});发布于 2015-03-17 05:45:45
我使用了我编写的hapi插件,它连接到mongo,处理记录错误,并添加了蓝鸟承诺。
'use strict';
var bluebird = require('bluebird');
var mongoose = bluebird.promisifyAll(require('mongoose'));
exports.register = function(plugin, options, next) {
mongoose.connect(options.mongo.uri, options.mongo.options, function (e) {
if (e) {
plugin.log(['error', 'database', 'mongodb'], 'Unable to connect to MongoDB: ' + e.message);
process.exit();
}
mongoose.connection.once('open', function () {
plugin.log(['info', 'database', 'mongodb'], 'Connected to MongoDB @ ' + options.mongo.uri);
});
mongoose.connection.on('connected', function () {
plugin.log(['info', 'database', 'mongodb'], 'Connected to MongoDB @ ' + options.mongo.uri);
});
mongoose.connection.on('error', function (e) {
plugin.log(['error', 'database', 'mongodb'], 'MongoDB ' + e.message);
});
mongoose.connection.on('disconnected', function () {
plugin.log(['warn', 'database', 'mongodb'], 'MongoDB was disconnected');
});
});
return next();
};
exports.register.attributes = {
name: 'mongoose',
version: '1.0.0'
};https://stackoverflow.com/questions/24954393
复制相似问题