2011-12-14 12 views
1

私は、この異なるURLにリクエストするRESTfulなアプリケーションを書いています。そのために私はnodejsを使用しており、異なるパスを設定するように表現しています。データベースとして、monogoDB(node-mongodb-native by christkv)の使用を計画しています。エクスプレスとmongoDBを持つNodeJSアプリ

私のコードは(つまり、モンゴの試みが含まれていません)次のようになります。

app.js

/** 
* Module dependencies. 
*/ 
var express = require('express') 
    , routes = require('./routes') 
var app = module.exports = express.createServer(); 

var Db = require('mongodb').Db; 
var Server = require('mongodb').Server; 
var client = new Db('test', new Server('127.0.0.1', 27017, {}));  

// Configuration 

app.configure(function() { 
    app.set('views', __dirname + '/views'); 
    app.use(express.bodyParser()); 
    app.use(express.methodOverride()); 
    app.use(app.router); 
    app.use(express.static(__dirname + '/public')); 
}); 

app.configure('development', function() { 
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
}); 

app.configure('production', function() { 
    app.use(express.errorHandler()); 
}); 

var insertData = function(err, collection) { 
    collection.insert({name: "Kristiono Setyadi"}); 
    collection.insert({name: "Meghan Gill"}); 
    collection.insert({name: "Spiderman"}); 
} 



// Routes 
app.post('/', routes.syncServiceIndex); 

app.post('/syncService', routes.synchServicePost); 
app.get('/syncService/:syncServiceUser/sync', routes.synchServiceSync); 
app.post('/syncService/:syncServiceUser/push', routes.synchServicePush); 
app.del('/syncService/:syncServiceUser', routes.synchServiceDel); 

app.post('/syncService/:syncServiceUser/contacts/push', routes.synchServiceContactsPush); 
app.get('/syncService/:syncServiceUser/contacts/sync', routes.synchServiceContactsSync); 

app.post('/syncService/:syncServiceUser/contacts/', routes.synchServiceContactsPost); 
app.get('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsGet); 
app.put('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsPut); 
app.del('/syncService/:syncServiceUser/contacts/:contactId', routes.synchServiceContactsDel); 

app.listen(3000); 



console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); 

と異なるファイルに異なるコンポーネントを維持するために、これはと私のファイルです各URLのためのコード:

index.js

//var ObjectID = db.bson_serializer.ObjectID; 
exports.syncServiceIndex = function(req, res) { 
    console.log('syncServiceIndex'); 
    //console.log("BODY:" + JSON.stringify(req.body)); 

    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServicePost = function(req, res) { 
    console.log('synchServicePost'); 
    console.log("BODY:" + JSON.stringify(req.body)); 
    var jsonObject = JSON.parse(JSON.stringify(req.body)); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceSync = function(req, res) { 
    console.log('synchServiceSync'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServicePush = function(req, res) { 
    console.log('synchServicePush'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceDel = function(req, res) { 
    console.log('synchServiceDel'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceDel = function(req, res) { 
    console.log('synchServiceDel'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsPush = function(req, res) { 
    console.log('synchServiceContactsPush'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsSync = function(req, res) { 
    console.log('synchServiceContactsSync'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsPost = function(req, res) { 
    console.log('synchServiceContactsPost'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsGet = function(req, res) { 
    console.log('synchServiceContactsGet'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsPut = function(req, res) { 
    console.log('synchServiceContactsPut'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

exports.synchServiceContactsDel = function(req, res) { 
    console.log('synchServiceContactsDel'); 
    res.statusCode = 200; 
    res.send("OK\n"); 
}; 

から何をI私がチェックアウトしてきたいくつかのサンプルコードで見た、私は本当に唯一のオープン接続を使用する必要があり、それは私が

client.open(function(err, pClient) { 

}); 

の呼び出しですべての私のコードを持っている必要があります。私が持っている問題は、index.jsファイルからデータベースを操作できるように、クライアントまたはコレクションを渡す方法がわからないということです。現在のレイアウトでこれを行う方法はありますか、いくつかのものを動かす必要がありますか?

答えて

1

あなたは、コールバックにすべてのあなたのルートをラップし、ミドルウェアにreq.mongoClient = pClientを設定し、例えば可能性:

client.open(function(err, pClient) { 
    clientMiddleware = function (req, res, next) { 
    req.mongoClient = pClient; 
    next(); 
    } 
    // your routes here, with clientMiddleware 
    app.post('/', clientMiddleware, routes.syncServiceIndex); 
    app.post('/syncService', clientMiddleware, routes.synchServicePost); 
    app.get('/syncService/:syncServiceUser/sync', clientMiddleware, routes.synchServiceSync); 
    // etc. 
}); 

あなたは今、すべてのルートでreq.mongoClientを使用して、クライアントを得ることができます。

+0

なぜclient.openコールバックで経路をラップする必要がありますか?私がモンゴスキンを使用する場合、これを行う必要がありますか? – georgesamper

+0

mongoClientをルート/コントローラで簡単にアクセスできるようにしたい場合は、これを実行します。これを行う必要はありません。好みの問題です。 – alessioalex

+0

ああ、私は参照してください。ありがとう:) – georgesamper

関連する問題