2011-12-14 72 views
1

我正在編寫這個小型RESTful應用程序,它將請求發送到不同的URL。爲此,我使用nodejs,並表示設置不同的路徑。作爲一個數據庫,我正在計劃使用monogoDB(由christkv提供的node-mongodb-native)。使用express和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"); 
}; 

從我看到一些示例代碼,我已經檢查了,我才真正只使用一個打開的連接,這是我應該有我所有的代碼在

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回調中包裝路由?如果我也使用mongoskin,我還必須這樣做嗎? – georgesamper 2012-06-10 13:50:00

相關問題