Tôi đã thực hiện một cách tiếp cận khác ở đây. Không phải nói rằng đó là tốt nhất, nhưng hãy để tôi giải thích.
- Mỗi lược đồ (và mô hình) nằm trong tệp (mô-đun) riêng của nó
- Mỗi nhóm tuyến đường cho một tài nguyên REST cụ thể nằm trong tệp (mô-đun) riêng của chúng
- Mỗi mô-đun tuyến đường chỉ cần
require
là mô hình Mongoose mà nó cần (chỉ 1) - Tệp chính (điểm nhập ứng dụng) chỉ
require
tất cả các mô-đun định tuyến để đăng ký chúng. - Kết nối Mongo nằm trong tệp gốc và được chuyển dưới dạng tham số cho bất kỳ kết nối nào cần.
Tôi có hai thư mục con dưới thư mục gốc ứng dụng của mình - routes
và các lược đồ schemas
.
Lợi ích của phương pháp này là:
- Bạn chỉ viết giản đồ một lần.
- Bạn không làm ô nhiễm tệp ứng dụng chính của mình khi đăng ký tuyến đường cho 4-5 tuyến đường trên mỗi tài nguyên REST (CRUD)
- Bạn chỉ xác định kết nối DB một lần
Đây là giao diện của một tệp giản đồ cụ thể:
Tệp:/schemas/theaterSchema.js
module.exports = function(db) {
return db.model('Theater', TheaterSchema());
}
function TheaterSchema () {
var Schema = require('mongoose').Schema;
return new Schema({
title: { type: String, required: true },
description: { type: String, required: true },
address: { type: String, required: true },
latitude: { type: Number, required: false },
longitude: { type: Number, required: false },
phone: { type: String, required: false }
});
}
Đây là cách bộ sưu tập các tuyến đường cho một tài nguyên cụ thể trông như thế nào:
Tệp:/routes/theaters.js
module.exports = function (app, options) {
var mongoose = options.mongoose;
var Schema = options.mongoose.Schema;
var db = options.db;
var TheaterModel = require('../schemas/theaterSchema')(db);
app.get('/api/theaters', function (req, res) {
var qSkip = req.query.skip;
var qTake = req.query.take;
var qSort = req.query.sort;
var qFilter = req.query.filter;
return TheaterModel.find().sort(qSort).skip(qSkip).limit(qTake)
.exec(function (err, theaters) {
// more code
});
});
app.post('/api/theaters', function (req, res) {
var theater;
theater.save(function (err) {
// more code
});
return res.send(theater);
});
app.get('/api/theaters/:id', function (req, res) {
return TheaterModel.findById(req.params.id, function (err, theater) {
// more code
});
});
app.put('/api/theaters/:id', function (req, res) {
return TheaterModel.findById(req.params.id, function (err, theater) {
// more code
});
});
app.delete('/api/theaters/:id', function (req, res) {
return TheaterModel.findById(req.params.id, function (err, theater) {
return theater.remove(function (err) {
// more code
});
});
});
};
Và đây là tệp ứng dụng gốc, đã khởi tạo kết nối và đăng ký tất cả các tuyến:
Tệp:app.js
var application_root = __dirname,
express = require('express'),
path = require('path'),
mongoose = require('mongoose'),
http = require('http');
var app = express();
var dbProduction = mongoose.createConnection('mongodb://here_insert_the_mongo_connection_string');
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use('/images/tmb', express.static(path.join(application_root, "images/tmb")));
app.use('/images/plays', express.static(path.join(application_root, "images/plays")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.get('/api', function (req, res) {
res.send('API is running');
});
var theatersApi = require('./routes/theaters')(app, { 'mongoose': mongoose, 'db': dbProduction });
// more code
app.listen(4242);
Hy vọng điều này hữu ích.