MongoDb에 연결하는 방법을 아는 사람은 누구입니까? Hapi.js 프레임 워크를 사용하고 계십니까?Hapi.js로 MongoDb에 연결하는 방법?
나는 그럭저럭 한 예 (https://github.com/Marsup/hapi-mongodb)를 찾을 수 있었지만, 플러그인을 사용해야하고 코드에 주석이 없습니다!
누구나 간단한 방법을 알고 있습니까?
MongoDb에 연결하는 방법을 아는 사람은 누구입니까? Hapi.js 프레임 워크를 사용하고 계십니까?Hapi.js로 MongoDb에 연결하는 방법?
나는 그럭저럭 한 예 (https://github.com/Marsup/hapi-mongodb)를 찾을 수 있었지만, 플러그인을 사용해야하고 코드에 주석이 없습니다!
누구나 간단한 방법을 알고 있습니까?
Blog post on user authentication with passport and Mongoose
이 또한 너무 읽고 플러그인 떨어져있는 게요의 모델을 기반으로 알고 있어야 자신의 건물에서 문서를 다시 읽어보십시오.
다음 (사용 몽구스가) 나를 위해 꽤 잘 작동합니다
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);
});
});
});
방문하면 리포 링크를 도울 수 http://cronj.com/blog/hapi-mongoose
전체 샘플 프로젝트 https://github.com/gauravgupta90/Hapi-Mongoose-Angular
Config.js
module.exports = {
server: {
host: '0.0.0.0',
port: 8000
},
database: {
host: '127.0.0.1',
port: 27017,
db: 'DatabaseName',
username: '',
password: ''
}
};
Server.js
var Hapi = require('hapi'),
Routes = require('./routes'),
config = require('./config'),
Db = require('./database');
var server = Hapi.createServer(config.server.host, config.server.port, {
cors: true
});
server.route(Routes.endpoints);
server.start(function() {
console.log('Server started ', server.info.uri);
});
Database.js는
var Mongoose = require('mongoose'),
config = require('./config');
Mongoose.connect('mongodb://' + config.database.host + '/' + config.database.db);
var db = Mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function callback() {
console.log("Connection with database succeeded.");
});
exports.Mongoose = Mongoose;
exports.db = db;
나는, 즉 몽고에 연결 썼다 로그 오류를 처리하고 블루 버드 약속을 추가하는 게요! 플러그인을 사용합니다.
'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'
};
더 간단하게 설명 할 수 있습니까? –
링크의 예제에서, 핸들러 함수를 작성할 때마다'var db = request.server.plugins [ 'hapi-mongodb'] .db;'와'var ObjectID = request.server '를 정의해야합니다. 플러그인 [ 'hapi-mongodb']. ObjectID;'. 그것은 시간이 지남에 성가 시게 될 것입니다. 내가 더 단순하게하고자하는 것은, 어떤 핸들러 함수의 외부에 하나의 연결 인스턴스를 갖는 것이다. 'var db = mongoose.connection()'과 같습니다. – hyprstack